Fórum Criação de componente #266964
01/02/2005
0
Eu quero o seguinte, quero colocar um Tlabel no topo onde será o titulo, até aí tudo bem.
implentei no oncreate Tpaneform:
constructor TPanelform.Create(AOwner: TComponent); var lbl:Tlabel; btn:Tbutton; begin inherited; if (csdesigning in ComponentState) then begin //criação do label -> Titulo lbl:= Tlabel.Create(AOwner); lbl.Name:= ´titulo´ + name; <- o name esta em branco lbl.parent:= self; with lbl do begin Caption:= name; height:= 21; Layout:= tlCenter; visible:=True; autosize := False; color:= clactivecaption; font.Color:= clCaptiontext; lbl.Align:= altop; end; //Criação do botão btn:= TButton.Create(Aowner); btn.Name:= ´Btnclose´ + name; <- name ainda está em branco btn.parent:= self; btn.Left:= width - 20; btn.onclick:= Destruir; <- não funciona with btn do begin caption:= ´X´; width:= 17; height:= 17; Top:= 3; end; end; end;
obs. Destruir é uma procedure; e o componente compila sem nenhum erro , hint e warinig.
Gostaria de que assim que mudasse o nome do TPanelForm o label e do button tabém fosse alterado
e gostaria de acrescentar o comando ´Tpanelform.destroy´ no ´OnClick´ do Tbutton.
Sistemald
Curtir tópico
+ 0Posts
01/02/2005
Massuda
type TPanelform = class(... protected procedure SetName(const NewName: TComponentName); override; // ... procedure TPanelform.SetName(const NewName: TComponentName); begin inherited SetName(NewName); // ajuste o nome do label e do botão de acordo com NewName end;
Gostei + 0
01/02/2005
Sistemald
Coloquei o seguinte
TPanelform = class(TPanel) private lbl:Tlabel; btn:Tbutton; Fcor:TColor; Ffontecor:TColor; FTitulo:String; Procedure Gettitulo(valor:string); Procedure GetCor(valor:Tcolor); Procedure GetFonteCor(valor:Tcolor);
e tb ...
published property Titulonome:String read Ftitulo write GetTitulo; property Titulocor:TColor read Fcor write GetCor Default clActiveCaption; property Titulofontcolor:TColor read Ffontecor write GetFonteCor Default clCaptionText;
Mas as propriedades sempre aperacem pretas e sem texto, não está respeintado o Default, e mesmo trocando no object inperctor não funciona, acredtido por estou tentando acessar as propriedades no oncreate.
Tem algum afterCreate?!?
Gostei + 0
01/02/2005
Sistemald
type
TPanelform = class(TPanel)
private
lbl:Tlabel;
btn:Tbutton;
Fcor:TColor;
Ffontecor:TColor;
FTitulo:String;
Procedure Gettitulo(valor:string);
Procedure GetCor(valor:Tcolor);
Procedure GetFonteCor(valor:Tcolor);
procedure criaoresto;
{ Private declarations }
protected
// procedure SetName(const NewName: TComponentName); override;
{ Protected declarations }
public
procedure Destruir(Sender: TObject);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Public declarations }
published
property Titulonome:String read Ftitulo write GetTitulo;
property Titulocor:TColor read Fcor write GetCor Default clActiveCaption;
property Titulofontcolor:TColor read Ffontecor write GetFonteCor Default clCaptionText;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents(´Donato´, [TPanelform]);
end;
{ TPanelform }
procedure TPanelform.GetCor(valor: Tcolor);
begin
FCor:= Valor;
Criaoresto;
end;
procedure TPanelform.GetFonteCor(valor: Tcolor);
begin
FFonteCor:= Valor;
end;
procedure TPanelform.Gettitulo(valor: string);
begin
FTitulo:= valor;
end;
constructor TPanelform.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TPanelform.Destroy;
begin
if not (csdesigning in ComponentState) then
begin
lbl.Destroy;
btn.Destroy;
end;
inherited;
end;
procedure TPanelform.Destruir(Sender: TObject);
begin
destroy;
end;
procedure TPanelform.criaoresto;
begin
if not (csdesigning in ComponentState) then
begin
//criação do label -> Titulo
lbl:= Tlabel.Create(self);
lbl.Name:= ´titulo´ + name;
lbl.parent:= self;
with lbl do
begin
Caption:= FTitulo;
height:= 21;
Layout:= tlCenter;
visible:=True;
autosize := False;
color:= Fcor;
font.Color:= Ffontecor;
lbl.Align:= altop;
end;
//Criação do botão
btn:= TButton.Create(self);
btn.Name:= ´Btnclose´ + name;
btn.parent:= self;
btn.Left:= width - 20;
with btn do
begin
caption:= ´X´;
width:= 17;
height:= 17;
Top:= 3;
onclick:= Destruir;
end;
end;
end;
Mas ainda não consegui usar a propriedade [b:f88bc2e360]Default[/b:f88bc2e360].
Gostei + 0
01/02/2005
Sistemald
type
TPanelform = class(TPanel)
private
lbl:Tlabel;
btn:Tbutton;
Fcor:TColor;
Ffontecor:TColor;
FTitulo:String;
Procedure Gettitulo(valor:string);
Procedure GetCor(valor:Tcolor);
Procedure GetFonteCor(valor:Tcolor);
procedure criaoresto;
{ Private declarations }
protected
// procedure SetName(const NewName: TComponentName); override;
{ Protected declarations }
public
procedure Destruir(Sender: TObject);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Public declarations }
published
property Titulonome:String read Ftitulo write GetTitulo;
property Titulocor:TColor read Fcor write GetCor Default clActiveCaption;
property Titulofontcolor:TColor read Ffontecor write GetFonteCor Default clCaptionText;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents(´Donato´, [TPanelform]);
end;
{ TPanelform }
procedure TPanelform.GetCor(valor: Tcolor);
begin
FCor:= Valor;
Criaoresto;
end;
procedure TPanelform.GetFonteCor(valor: Tcolor);
begin
FFonteCor:= Valor;
end;
procedure TPanelform.Gettitulo(valor: string);
begin
FTitulo:= valor;
end;
constructor TPanelform.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TPanelform.Destroy;
begin
if not (csdesigning in ComponentState) then
begin
lbl.Destroy;
btn.Destroy;
end;
inherited;
end;
procedure TPanelform.Destruir(Sender: TObject);
begin
destroy;
end;
procedure TPanelform.criaoresto;
begin
if not (csdesigning in ComponentState) then
begin
//criação do label -> Titulo
lbl:= Tlabel.Create(self);
lbl.Name:= ´titulo´ + name;
lbl.parent:= self;
with lbl do
begin
Caption:= FTitulo;
height:= 21;
Layout:= tlCenter;
visible:=True;
autosize := False;
color:= Fcor;
font.Color:= Ffontecor;
lbl.Align:= altop;
end;
//Criação do botão
btn:= TButton.Create(self);
btn.Name:= ´Btnclose´ + name;
btn.parent:= self;
btn.Left:= width - 20;
with btn do
begin
caption:= ´X´;
width:= 17;
height:= 17;
Top:= 3;
onclick:= Destruir;
end;
end;
end;
Mas ainda não consegui usar a propriedade [b:b64d8fce25]Default[/b:b64d8fce25].
Gostei + 0
01/02/2005
Sistemald
type
TPanelform = class(TPanel)
private
lbl:Tlabel;
btn:Tbutton;
Fcor:TColor;
Ffontecor:TColor;
FTitulo:String;
Procedure Gettitulo(valor:string);
Procedure GetCor(valor:Tcolor);
Procedure GetFonteCor(valor:Tcolor);
procedure criaoresto;
{ Private declarations }
protected
// procedure SetName(const NewName: TComponentName); override;
{ Protected declarations }
public
procedure Destruir(Sender: TObject);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Public declarations }
published
property Titulonome:String read Ftitulo write GetTitulo;
property Titulocor:TColor read Fcor write GetCor Default clActiveCaption;
property Titulofontcolor:TColor read Ffontecor write GetFonteCor Default clCaptionText;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents(´Donato´, [TPanelform]);
end;
{ TPanelform }
procedure TPanelform.GetCor(valor: Tcolor);
begin
FCor:= Valor;
Criaoresto;
end;
procedure TPanelform.GetFonteCor(valor: Tcolor);
begin
FFonteCor:= Valor;
end;
procedure TPanelform.Gettitulo(valor: string);
begin
FTitulo:= valor;
end;
constructor TPanelform.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TPanelform.Destroy;
begin
if not (csdesigning in ComponentState) then
begin
lbl.Destroy;
btn.Destroy;
end;
inherited;
end;
procedure TPanelform.Destruir(Sender: TObject);
begin
destroy;
end;
procedure TPanelform.criaoresto;
begin
if not (csdesigning in ComponentState) then
begin
//criação do label -> Titulo
lbl:= Tlabel.Create(self);
lbl.Name:= ´titulo´ + name;
lbl.parent:= self;
with lbl do
begin
Caption:= FTitulo;
height:= 21;
Layout:= tlCenter;
visible:=True;
autosize := False;
color:= Fcor;
font.Color:= Ffontecor;
lbl.Align:= altop;
end;
//Criação do botão
btn:= TButton.Create(self);
btn.Name:= ´Btnclose´ + name;
btn.parent:= self;
btn.Left:= width - 20;
with btn do
begin
caption:= ´X´;
width:= 17;
height:= 17;
Top:= 3;
onclick:= Destruir;
end;
end;
end;
Mas ainda não consegui usar a propriedade [b:82e36bc333]Default[/b:82e36bc333].
Gostei + 0
01/02/2005
Massuda
type TPanelform = class(TPanel) // ... published property Titulocor:TColor read Fcor write GetCor Default clActiveCaption;
constructor TPanelform.Create(AOwner: TComponent); begin inherited; FCor := clActiveCaption; end;
Gostei + 0
01/02/2005
Sistemald
achei um ótimo artigo
http://www.clubedelphi.net/artigos/criacao_componentes.asp
mas ainda falta uma maneira de executar uma procedure logo após o create do componente.
Gostei + 0
01/02/2005
Massuda
Outra coisa... procure não chamar diretamente o destrutor Destroy, mas sim usar a procedure Free, que é a prática recomendada. Dessa forma, seu destrutor ficaria assim:
destructor TPanelform.Destroy; begin lbl.Free; btn.Free; inherited; end;
Gostei + 0
01/02/2005
Sistemald
lbl.parent:= self;
Preciso de algo do tipo
lbl.parent:= TPanelForm;
deste jeito assim que destruir o TpanelForm seus Tlabel e o Tbutton será destruido automaticamente.
E a respeito do Criaoreto no [b:4f5b215dc5]oncreate[/b:4f5b215dc5] não funciona pois a var vem em branco ou com a opção default.
Gostei + 0
01/02/2005
Massuda
lbl:= TLabel.Create(Self);
Você incluiu no código da procedure CriaOResto e do destrutor Destroy o seguinte:
if not (csdesigning in ComponentState) ...
Gostei + 0
02/02/2005
Sistemald
e as duas Tcolor vem com o default, não importando a o que se coloca no object inpector.
fiz um timer para ser executado depois do oncreate e funcionou
mas acredito ter um jeito melhor
Gostei + 0
02/02/2005
Sistemald
No compo tem o bto fechar com o código:
Free;
Quando ele é pressionado da [b:0fd01d523a]Violação de memória[/b:0fd01d523a]
Mas se no em um projeto eu coloco um botão fora do compo com o mesmo código da certo, sem erros.
Alguém tem uma dica
Gostei + 0
02/02/2005
Marcelo Saviski
Desse artigo, acho que muita gente não reparou, mais tem a 2º parte
http://www.clubedelphi.net/artigos/criacao_componentes2.asp
Gostei + 0
21/02/2005
Sistemald
Alguém sabe como resolver isto? :?: :?: :?: :?:
Gostei + 0
21/02/2005
Sistemald
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)