Como Saber o Caption de um Tlabel atraves do focus de um Tedit
Amigos
Tenho um componente Tlabel que Faz a propriedade FocusControl para um edit, eu quero saber como posso Acessar a propriedade Caption do Label.
Abracos
Tenho um componente Tlabel que Faz a propriedade FocusControl para um edit, eu quero saber como posso Acessar a propriedade Caption do Label.
Abracos
Eduardo Richeli
Curtidas 0
Respostas
Thiago Santana
12/03/2010
Eduardo não entendi...
Vc poderia explicar mais detalhadamente? Como vc está passando essa propriedade FocusControl para um edit?
E vc que acessar a propriedade caption do label atráves de que?
Poste aí para que possamos te ajudar...
Abraçoo
GOSTEI 0
Wilson Junior
12/03/2010
Crie a rotina:
function Ret_CaptionLabel(CompFocusControl: TWincontrol): string;
var
x: integer;
begin
Result := '';
for x := 0 to CompFocusControl.Parent.ComponentCount - 1 do
begin
if ( CompFocusControl.Parent.Components[x] is TLabel )
and ( (CompFocusControl.Parent.Components[x] as TLabel).FocusControl = CompFocusControl ) then
begin
Result := (CompFocusControl.Parent.Components[x] as TLabel).Caption;
Break;
end
;
end;
end;
Como chamar a rotina:
ShowMessage( Ret_CaptionLabel(MeuEdit) );
ShowMessage( Ret_CaptionLabel(MeuCombBox) );
ShowMessage( Ret_CaptionLabel(MeuBitbtn) );
OBS.: para funcionar esta rotina, o TLabel deve estar no mesmo componente pai que o componente passado como parâmetro (CompFocusControl).
Exemplo1: tenho um TPanel e dentro dele tenho o TLabel e o TEdit, assim funciona;
Exemplo2: tenho um TPanel e dentro dele tenho o TLabel e o TEdit esta for do TPanel, ou vice-versa, assim NÃO funciona.
Espero ter colaborado.
function Ret_CaptionLabel(CompFocusControl: TWincontrol): string;
var
x: integer;
begin
Result := '';
for x := 0 to CompFocusControl.Parent.ComponentCount - 1 do
begin
if ( CompFocusControl.Parent.Components[x] is TLabel )
and ( (CompFocusControl.Parent.Components[x] as TLabel).FocusControl = CompFocusControl ) then
begin
Result := (CompFocusControl.Parent.Components[x] as TLabel).Caption;
Break;
end
;
end;
end;
Como chamar a rotina:
ShowMessage( Ret_CaptionLabel(MeuEdit) );
ShowMessage( Ret_CaptionLabel(MeuCombBox) );
ShowMessage( Ret_CaptionLabel(MeuBitbtn) );
OBS.: para funcionar esta rotina, o TLabel deve estar no mesmo componente pai que o componente passado como parâmetro (CompFocusControl).
Exemplo1: tenho um TPanel e dentro dele tenho o TLabel e o TEdit, assim funciona;
Exemplo2: tenho um TPanel e dentro dele tenho o TLabel e o TEdit esta for do TPanel, ou vice-versa, assim NÃO funciona.
Espero ter colaborado.
GOSTEI 0
Marcos Iwazaki
12/03/2010
Esta ae amigo.
Se eu entendi bem vc tem o edit e quer acessar o label q tem o focus control p esse edit, ne?
bom se for isso...
for i:= 0 to Components.ComponentCount-1 do begin
if Components[i] is TLabel then begin
If Tlabel(Components[i]).FocusControl.Name = 'MeuEdit' then begin
// ------------ aqui vc tem o label q tem o MeuEdit como FocusControl
TLabel(Components[i]).Caption := 'Caption do MeuEdit';
end;
end;
end;
bom talvez tenha erros de sintaxe pois fiz de cabeça...
mas é algo parecido com isso.. se eu entendi o que vc queria.
Se eu entendi bem vc tem o edit e quer acessar o label q tem o focus control p esse edit, ne?
bom se for isso...
for i:= 0 to Components.ComponentCount-1 do begin
if Components[i] is TLabel then begin
If Tlabel(Components[i]).FocusControl.Name = 'MeuEdit' then begin
// ------------ aqui vc tem o label q tem o MeuEdit como FocusControl
TLabel(Components[i]).Caption := 'Caption do MeuEdit';
end;
end;
end;
bom talvez tenha erros de sintaxe pois fiz de cabeça...
mas é algo parecido com isso.. se eu entendi o que vc queria.
Amigos
Tenho um componente Tlabel que Faz a propriedade FocusControl para um edit, eu quero saber como posso Acessar a propriedade Caption do Label.
Abracos
Tenho um componente Tlabel que Faz a propriedade FocusControl para um edit, eu quero saber como posso Acessar a propriedade Caption do Label.
Abracos
GOSTEI 0
Emerson Nascimento
12/03/2010
rotina:
function TForm12.RetornaLabelFoco(Controle: TWinControl): TLabel;
var
i: integer;
begin
Result := nil;
for i := 0 to ControlCount - 1 do
if (Controls[i] is TLabel) and (TLabel(Controls[i]).FocusControl = Controle) then
begin
Result := TLabel(Controls[i]);
exit;
end;
end;
uso:
procedure TForm12.Button2Click(Sender: TObject);
var
lbl: TLabel;
begin
lbl := RetornaLabelFoco(Edit1);
if lbl <> nil then
begin
ShowMessage(lbl.Name);
lbl.Caption := 'Label Edit1';
end;
lbl := RetornaLabelFoco(Memo1);
if lbl <> nil then
begin
ShowMessage(lbl.Name);
lbl.Caption := 'Label Memo1';
end;
end;
function TForm12.RetornaLabelFoco(Controle: TWinControl): TLabel;
var
i: integer;
begin
Result := nil;
for i := 0 to ControlCount - 1 do
if (Controls[i] is TLabel) and (TLabel(Controls[i]).FocusControl = Controle) then
begin
Result := TLabel(Controls[i]);
exit;
end;
end;
uso:
procedure TForm12.Button2Click(Sender: TObject);
var
lbl: TLabel;
begin
lbl := RetornaLabelFoco(Edit1);
if lbl <> nil then
begin
ShowMessage(lbl.Name);
lbl.Caption := 'Label Edit1';
end;
lbl := RetornaLabelFoco(Memo1);
if lbl <> nil then
begin
ShowMessage(lbl.Name);
lbl.Caption := 'Label Memo1';
end;
end;
GOSTEI 0
Eduardo Richeli
12/03/2010
vou fazer os testes, obrigado pessoal
GOSTEI 0
Eduardo Richeli
12/03/2010
amigos ha um pequeno detalhe, o Tedit e um componente , neste caso acho que terei que acessar o forumulario que ele se encontra...
GOSTEI 0
Emerson Nascimento
12/03/2010
todos as soluções apresentadas preveem que o TEdit seja um componente.
desculpe, mas não entendi o que este detalhe acrescenta....
desculpe, mas não entendi o que este detalhe acrescenta....
GOSTEI 0
Eduardo Richeli
12/03/2010
todos as soluções apresentadas preveem que o TEdit seja um componente.
desculpe, mas não entendi o que este detalhe acrescenta....
desculpe, mas não entendi o que este detalhe acrescenta....
acho que nestes exemplos tenho que ter acesso ao form.
GOSTEI 0