GARANTIR DESCONTO

Fórum Criação de componentes #342197

22/05/2007

0

pessoal estou tentanto criar um componete dentro de outro,
não sei se é possivel mais a idéia é criar um componente com o ancestor type = TPanel ai dentro deste panel criar um DBGrid.

consigo fazer isso via código, tipo no onCreate de um form:
begin
  pnl := TPanel.Create(self);
  pnl.Parent := self;
  pnl.Width := 400;
  pnl.Height := 300;
  pnl.Left := (self.Width-pnl.Width) div 2;
  pnl.Top := (self.Height-pnl.Height) div 2;
  // ...

  grid := TDBGrid.Create(self);
  grid.Parent := pnl;
  grid.Align := alClient;
end;


como criar um panel que já contenha um DBGrid dentro dele ??


Fabiano Góes

Fabiano Góes

Responder

Posts

22/05/2007

Adriano Santos

[quote:ef0d4a1415=´Fabiano Góes´]pessoal estou tentanto criar um componete dentro de outro,
não sei se é possivel mais a idéia é criar um componente com o ancestor type = TPanel ai dentro deste panel criar um DBGrid.

consigo fazer isso via código, tipo no onCreate de um form:
begin
  pnl := TPanel.Create(self);
  pnl.Parent := self;
  pnl.Width := 400;
  pnl.Height := 300;
  pnl.Left := (self.Width-pnl.Width) div 2;
  pnl.Top := (self.Height-pnl.Height) div 2;
  // ...

  grid := TDBGrid.Create(pnl);
  grid.Parent := pnl;
  grid.Align := alClient;
end;


como criar um panel que já contenha um DBGrid dentro dele ??[/quote:ef0d4a1415]
Fabiano, tô fora do Delphi agora e nao tenho como testar, mas se nao me engano no Create do segundo componente vc tem que dar o nome do container dele assim:

grid := TDBGrid.Create(pnl);
grid.Parent := pnl;


Flw


Responder

Gostei + 0

22/05/2007

Fabiano Góes

Adriano isso mesmo assim funcionou,
agora tenho outra duvida:

quando eu crio uma instancia do meu componente via código funciona:
var
  comp: TMeuComponent;
begin
  comp := TMeuComponent.Create(self);
  comp.Parent := Form1;
end;


agora quando eu instalo o meu componente na palheta do delphi e coloco um componente em um form aparentemente está tudo certo, vem um panel com uma grid um edit etc ...

mais quando executo o programa da um erro: [color=red:4418aae73a]Class TPanel not found[/color:4418aae73a]

como eu posso setar a propriedade Parent do meu componente para o form q vou usa-lo ?


Responder

Gostei + 0

22/05/2007

Adriano Santos

Afff, hoje tô no chutômetro, rsrs, não leve a mal. É que estou sem Delphi.
Bem, se não me falham o tico e o teco você tem dar dizer que o OWner do component é o form assim


begin
  pnl := TPanel.Create(Self.OWner);
  ...
  grid := TDBGrid.Create(pnl);
  grid.Parent := pnl;
  grid.Align := alClient;
end; 


Isso no Constructor do component.


Responder

Gostei + 0

22/05/2007

Fabiano Góes

Adriano,
como meu componente é herdado de TPanel eu não faço:
  pnl := TPanel.Create(Self.OWner);


o problema esta sendo setar o Parent do meu componente,
como posso fazer: self.Parent := form no código pois ainda não sei como será o nome do form que vai ter o meu componente.

cara não manjo muito de criação de componentes estou perdidão :lol:


Responder

Gostei + 0

22/05/2007

Adriano Santos

Ahhhh, ai que tah a brincadeira velhinho.
Fiz um componentinho véio aqui de última hora. Deu certo, dá uma olhada no código ´compreto´ dele.

Olha o constructor:

constructor TPanelComGrid.Create(AOwner: TComponent);
begin
  inherited;
  FGrid := TDBGrid.Create(AOwner);
  FGrid.Parent := Self;
  FGrid.Left := 5;
  FGrid.Top := 5;
  FGrid.Width := Self.Width - 10;
  FGrid.Height := Self.Height - 10;
end;


unit PanelWithGrid;

interface

uses
  SysUtils, Classes, Controls, ExtCtrls, Grids, DbGrids;

type
  TPanelComGrid = class(TPanel)
  private
    { Private declarations }
    FGrid : TDbGrid;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent);override;
    destructor Destroy;override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents(´Samples´, [TPanelComGrid]);
end;

{ TPanelComGrid }

constructor TPanelComGrid.Create(AOwner: TComponent);
begin
  inherited;
  FGrid := TDBGrid.Create(AOwner);
  FGrid.Parent := Self;
  FGrid.Left := 5;
  FGrid.Top := 5;
  FGrid.Width := Self.Width - 10;
  FGrid.Height := Self.Height - 10;
end;

destructor TPanelComGrid.Destroy;
begin
  FGrid.Free;
  inherited;
end;

end.



Responder

Gostei + 0

22/05/2007

Adriano Santos

Só lembrando> Self dentro do seu componente será sempre a classe principal, ou seja, no meu e o no seu caso o TPanel herdado. Se usarmos Self.Owner então é o dono do componente, 1 cara acima dele que pode ser o form onde ele está alocado ou outro componente container.

flw


Responder

Gostei + 0

23/05/2007

Fabiano Góes

Bom galera, consegui criar o componente desde já quero agradecer ao amigo Adriano valeu ai mano.

o componente é um TPanel com outros componentes dentro dele:
1 Edit
1 Label
1 DBGrid
1 DBNavigator
1 Botton

o componente serve para realizar transações em base de dados xml;
para usar:
propriedade DataBase = arquivo xml
propriedade KeiField = campo para realizar locates

no onCreate do form:
ContainerLocate1.ClientDataSet.LoadFromFile(ContainerLocate1.DataBase);


ao executar o programa já é possivel: incluir, alterar, deletar, locate.

eis o código caso alguem queira aproveitar:
unit ContainerLocate;

interface

uses
  SysUtils, Classes, Controls, ExtCtrls, DBGrids, StdCtrls, Forms, DBClient,
  Windows, DB, DBCtrls, Dialogs;

type
  TContainerLocate = class(TPanel)
  private
    FGrid: TDBGrid;
    FPanelTopo: TPanel;
    FLabelLocate: TLabel;
    FEditLocate: TEdit;
    FKeyField: string;
    FDataSource: TDataSource;
    FClientDataSet: TClientDataSet;
    FNavigator: TDBNavigator;
    FPanelBotton: TPanel;
    FBtnAplly: TButton;
    FDataBase: string;
    procedure SetGrid(const Value: TDBGrid);
    procedure SetPanelTopo(const Value: TPanel);
    procedure SetLabelLocate(const Value: TLabel);
    procedure SetEditLocate(const Value: TEdit);
    procedure SetKeyField(const Value: string);
    procedure SetClientDataSet(const Value: TClientDataSet);
    procedure SetNavigator(const Value: TDBNavigator);
    procedure SetPanelBotton(const Value: TPanel);
    procedure SetBtnAplly(const Value: TButton);
    procedure SetDataBase(const Value: string);
    { Private declarations }
  protected
    { Protected declarations }
  public
    constructor Create(AOwner: TComponent); override;
    procedure EditChange(Sender: TObject);
    procedure BtnApplyClick(Sender: TObject);
    { Public declarations }
  published
    property PanelTopo: TPanel read FPanelTopo write SetPanelTopo;
    property PanelBotton: TPanel read FPanelBotton write SetPanelBotton;
    property LabelLocate: TLabel read FLabelLocate write SetLabelLocate;
    property EditLocate: TEdit read FEditLocate write SetEditLocate;
    property Grid: TDBGrid read FGrid write SetGrid;
    property KeyField: string read FKeyField write SetKeyField;
    property ClientDataSet: TClientDataSet read FClientDataSet write SetClientDataSet;
    property Navigator: TDBNavigator read FNavigator write SetNavigator;
    property BtnAplly: TButton read FBtnAplly write SetBtnAplly;
    property DataBase: string read FDataBase write SetDataBase;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents(´My Components´, [TContainerLocate]);
end;

{ TContanerLocate }

constructor TContainerLocate.Create(AOwner: TComponent);
begin
  inherited;
  self.Width      := 500;
  self.Height     := 400;
  self.Top        := 10;
  self.Left       := 10;
  self.BevelInner := bvRaised;
  self.BevelOuter := bvLowered;

  PanelTopo := TPanel.Create(Self);
  PanelTopo.Parent     := Self;
  PanelTopo.Align      := alTop;
  PanelTopo.BevelInner := bvRaised;
  PanelTopo.BevelOuter := bvLowered;

  PanelBotton := TPanel.Create(Self);
  PanelBotton.Parent     := Self;
  PanelBotton.Align      := alBottom;
  PanelBotton.BevelInner := bvRaised;
  PanelBotton.BevelOuter := bvLowered;
  PanelBotton.Height     := 40;

  BtnAplly := TButton.Create(self);
  BtnAplly.Parent := PanelBotton;
  BtnAplly.Caption := ´Apply´;
  BtnAplly.Height := 30;
  BtnAplly.Top := 5;
  BtnAplly.Width := 85;
  BtnAplly.OnClick := BtnApplyClick;

  LabelLocate := TLabel.Create(Self);
  LabelLocate.Parent := PanelTopo;
  LabelLocate.Caption:= ´Digite o Parametro´;
  LabelLocate.Top    := (PanelTopo.Height-LabelLocate.Height) div 2;
  LabelLocate.Left   := 10;

  EditLocate := TEdit.Create(Self);
  EditLocate.Parent := PanelTopo;
  EditLocate.Text   := ´´;
  EditLocate.Top    := 12;
  EditLocate.Left   := 10 + LabelLocate.Width + 10;
  EditLocate.Width  := Self.Width - LabelLocate.Width - 30;
  EditLocate.BevelKind := bkFlat;
  EditLocate.BorderStyle := bsNone;
  EditLocate.OnChange := EditChange;

  FClientDataSet := TClientDataSet.Create(self);

  FDataSource := TDataSource.Create(self);
  FDataSource.DataSet := FClientDataSet;

  Grid := TDBGrid.Create(Self);
  Grid.Parent := self;
  Grid.Align  := alClient;
  Grid.DataSource := FDataSource;

  Navigator := TDBNavigator.Create(self);
  Navigator.Parent := PanelBotton;
  Navigator.Align := alCustom;
  Navigator.Left := 5;
  Navigator.Top := 5;
  Navigator.Height := 30;
  Navigator.Width := 400;
  Navigator.VisibleButtons := [nbFirst, nbPrior, nbNext, nbLast, nbInsert, nbDelete, nbEdit, nbPost, nbCancel];
  Navigator.DataSource := FDataSource;

  BtnAplly.Left := 5 + Navigator.Width;
end;

procedure TContainerLocate.EditChange(Sender: TObject);
begin
  inherited;
  Grid.DataSource.DataSet.FindField(KeyField);
  Grid.DataSource.DataSet.Locate(KeyField, EditLocate.Text, [loCaseInsensitive, loPartialKey]);
end;

procedure TContainerLocate.BtnApplyClick(Sender: TObject);
begin
  try
    if ClientDataSet.ProviderName <> ´´ then
      ClientDataSet.ApplyUpdates(0)
    else
      ClientDataSet.SaveToFile(DataBase);

    MessageDlg(´Regostro salvo com sucesso !´, mtInformation, [mbOK], 0);
  except
    MessageDlg(´Erro ao tentar salvar a operação´, mtError, [mbOK], 0);
  end;
end;

procedure TContainerLocate.SetEditLocate(const Value: TEdit);
begin
  FEditLocate := Value;
end;

procedure TContainerLocate.SetGrid(const Value: TDBGrid);
begin
  FGrid := Value;
end;

procedure TContainerLocate.SetKeyField(const Value: string);
begin
  FKeyField := Value;
end;

procedure TContainerLocate.SetLabelLocate(const Value: TLabel);
begin
  FLabelLocate := Value;
end;

procedure TContainerLocate.SetPanelTopo(const Value: TPanel);
begin
  FPanelTopo := Value;
end;

procedure TContainerLocate.SetClientDataSet(const Value: TClientDataSet);
begin
  FClientDataSet := Value;
end;

procedure TContainerLocate.SetNavigator(const Value: TDBNavigator);
begin
  FNavigator := Value;
end;

procedure TContainerLocate.SetPanelBotton(const Value: TPanel);
begin
  FPanelBotton := Value;
end;

procedure TContainerLocate.SetBtnAplly(const Value: TButton);
begin
  FBtnAplly := Value;
end;

procedure TContainerLocate.SetDataBase(const Value: string);
begin
  FDataBase := Value;
end;

end.


se alguem quizer implementar mais coisas e quizer compartilhar fique a vontade.

abraço galera !!!


Responder

Gostei + 0

23/05/2007

Adriano Santos

Muito bom velhinho. Aliás, vou copiar pq estamos criando uma série de componentes pra implementar nossa aplicação n-tier. Nela precisaremos de diversos componentes como este pra substituir componentes como LMD, RxLib e Jedi. vlw mesmo.


Responder

Gostei + 0

24/05/2007

Fabiano Góes

Dei uma implementadinha,
agora ao invés de colocar esse objeto em um form crieu um evento: Execute que cria um form em tempo de execução com todas as caracteristicas que ja existia:

unit FormLocate;

interface

uses
  SysUtils, Classes, Controls, ExtCtrls, DBGrids, StdCtrls, Forms, DBClient,
  Windows, DB, DBCtrls, Dialogs;

type
  TLocate = class(TComponent)
  private
    FGrid: TDBGrid;
    FPanelTopo: TPanel;
    FLabelLocate: TLabel;
    FEditLocate: TEdit;
    FKeyField: string;
    FDataSource: TDataSource;
    FClientDataSet: TClientDataSet;
    FNavigator: TDBNavigator;
    FPanelBotton: TPanel;
    FBtnAplly: TButton;
    FDataBase: string;
    FPanelCentral: TPanel;
    FForm: TForm;
    procedure SetGrid(const Value: TDBGrid);
    procedure SetPanelTopo(const Value: TPanel);
    procedure SetLabelLocate(const Value: TLabel);
    procedure SetEditLocate(const Value: TEdit);
    procedure SetKeyField(const Value: string);
    procedure SetClientDataSet(const Value: TClientDataSet);
    procedure SetNavigator(const Value: TDBNavigator);
    procedure SetPanelBotton(const Value: TPanel);
    procedure SetBtnAplly(const Value: TButton);
    procedure SetDataBase(const Value: string);
    procedure SetPanelCentral(const Value: TPanel);
    procedure SetForm(const Value: TForm);
    { Private declarations }
  protected
    { Protected declarations }
  public
    constructor Create(AOwner: TComponent); override;
    procedure EditChange(Sender: TObject);
    procedure BtnApplyClick(Sender: TObject);
    procedure Execute;
    { Public declarations }
  published
    property Form: TForm read FForm write SetForm;
    property PanelCentral: TPanel read FPanelCentral write SetPanelCentral;
    property PanelTopo: TPanel read FPanelTopo write SetPanelTopo;
    property PanelBotton: TPanel read FPanelBotton write SetPanelBotton;
    property LabelLocate: TLabel read FLabelLocate write SetLabelLocate;
    property EditLocate: TEdit read FEditLocate write SetEditLocate;
    property Grid: TDBGrid read FGrid write SetGrid;
    property KeyField: string read FKeyField write SetKeyField;
    property ClientDataSet: TClientDataSet read FClientDataSet write SetClientDataSet;
    property Navigator: TDBNavigator read FNavigator write SetNavigator;
    property BtnAplly: TButton read FBtnAplly write SetBtnAplly;
    property DataBase: string read FDataBase write SetDataBase;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents(´My Components´, [TLocate]);
end;

{ TLocate }

constructor TLocate.Create(AOwner: TComponent);
begin
  inherited;
  Form := TForm.Create(self);
  Form.Position := poScreenCenter;
  Form.Width := 500;
  Form.Height := 400;

  PanelCentral := TPanel.Create(self);
  PanelCentral.Parent := Form;
  PanelCentral.BevelInner := bvRaised;
  PanelCentral.BevelOuter := bvLowered;
  PanelCentral.Align      := alClient;

  PanelTopo := TPanel.Create(Self);
  PanelTopo.Parent     := PanelCentral;
  PanelTopo.Align      := alTop;
  PanelTopo.BevelInner := bvRaised;
  PanelTopo.BevelOuter := bvLowered;

  PanelBotton := TPanel.Create(Self);
  PanelBotton.Parent     := PanelCentral;
  PanelBotton.Align      := alBottom;
  PanelBotton.BevelInner := bvRaised;
  PanelBotton.BevelOuter := bvLowered;
  PanelBotton.Height     := 40;

  BtnAplly := TButton.Create(self);
  BtnAplly.Parent := PanelBotton;
  BtnAplly.Caption := ´Apply´;
  BtnAplly.Height := 30;
  BtnAplly.Top := 5;
  BtnAplly.Width := 85;
  BtnAplly.OnClick := BtnApplyClick;

  LabelLocate := TLabel.Create(Self);
  LabelLocate.Parent := PanelTopo;
  LabelLocate.Caption:= ´Digite o Parametro´;
  LabelLocate.Top    := (PanelTopo.Height-LabelLocate.Height) div 2;
  LabelLocate.Left   := 10;

  EditLocate := TEdit.Create(Self);
  EditLocate.Parent := PanelTopo;
  EditLocate.Text   := ´´;
  EditLocate.Top    := 12;
  EditLocate.Left   := 10 + LabelLocate.Width + 10;
  EditLocate.Width  := PanelCentral.Width - LabelLocate.Width - 30;
  EditLocate.BevelKind := bkFlat;
  EditLocate.BorderStyle := bsNone;
  EditLocate.OnChange := EditChange;

  FClientDataSet := TClientDataSet.Create(self);

  FDataSource := TDataSource.Create(self);
  FDataSource.DataSet := FClientDataSet;

  Grid := TDBGrid.Create(Self);
  Grid.Parent := PanelCentral;
  Grid.Align  := alClient;
  Grid.DataSource := FDataSource;

  Navigator := TDBNavigator.Create(self);
  Navigator.Parent := PanelBotton;
  Navigator.Align := alCustom;
  Navigator.Left := 5;
  Navigator.Top := 5;
  Navigator.Height := 30;
  Navigator.Width := 400;
  Navigator.VisibleButtons := [nbFirst, nbPrior, nbNext, nbLast, nbInsert, nbDelete, nbEdit, nbPost, nbCancel];
  Navigator.DataSource := FDataSource;

  BtnAplly.Left := 5 + Navigator.Width;
end;

procedure TLocate.EditChange(Sender: TObject);
begin
  inherited;
  Grid.DataSource.DataSet.FindField(KeyField);
  Grid.DataSource.DataSet.Locate(KeyField, EditLocate.Text, [loCaseInsensitive, loPartialKey]);
end;

procedure TLocate.BtnApplyClick(Sender: TObject);
begin
  try
    if ClientDataSet.ProviderName <> ´´ then
      ClientDataSet.ApplyUpdates(0)
    else
      ClientDataSet.SaveToFile(DataBase);

    MessageDlg(´Registro salvo com sucesso !´, mtInformation, [mbOK], 0);
  except
    MessageDlg(´Erro ao tentar salvar a operação´, mtError, [mbOK], 0);
  end;
end;

procedure TLocate.SetEditLocate(const Value: TEdit);
begin
  FEditLocate := Value;
end;

procedure TLocate.SetGrid(const Value: TDBGrid);
begin
  FGrid := Value;
end;

procedure TLocate.SetKeyField(const Value: string);
begin
  FKeyField := Value;
end;

procedure TLocate.SetLabelLocate(const Value: TLabel);
begin
  FLabelLocate := Value;
end;

procedure TLocate.SetPanelTopo(const Value: TPanel);
begin
  FPanelTopo := Value;
end;

procedure TLocate.SetClientDataSet(const Value: TClientDataSet);
begin
  FClientDataSet := Value;
end;

procedure TLocate.SetNavigator(const Value: TDBNavigator);
begin
  FNavigator := Value;
end;

procedure TLocate.SetPanelBotton(const Value: TPanel);
begin
  FPanelBotton := Value;
end;

procedure TLocate.SetBtnAplly(const Value: TButton);
begin
  FBtnAplly := Value;
end;

procedure TLocate.SetDataBase(const Value: string);
begin
  FDataBase := Value;
end;

procedure TLocate.SetPanelCentral(const Value: TPanel);
begin
  FPanelCentral := Value;
end;

procedure TLocate.SetForm(const Value: TForm);
begin
  FForm := Value;
end;

procedure TLocate.Execute;
begin
  Form.ShowModal;
end;

end.



Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar