Acessar label propriedade de um label criado em run-time
23/02/2021
0
Estou criando alguns TCategoryPanel e dentro dele possuo alguns label's, a criação é feita corretamente,
porém preciso acessar o valor do label criado em determinado TCategoryPanel.
Já fiz a implementação do evento "OnDblClick" porém não sei como pegar o valor do Label de determinado Painel criado.
Detalhe, possuo um CategoryPanelGroup dentro dele faço a criação dos CategoryPanel e Label.
Bruno Divino
Post mais votado
23/02/2021
var lblPedido: TComponent; cSufLabel: string; begin cSufLabel := Copy(TWinControl(Sender).Name,7,50); // obtém o conteúdo depois de 'CadPed' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_NRO_PD'+cSufLabel); if assigned(lblPedido) then ShowMessage( TLabel(lblPedido).Caption ); end;
Emerson Nascimento
Mais Posts
23/02/2021
Emerson Nascimento
se o label é nomeado, busque pelo nome.
23/02/2021
Gxf
if label1.Tag = 0 then
ShowMessage('é tag 0')
else
ShowMessage('tag <> de 0');
23/02/2021
Bruno Divino
se o label é nomeado, busque pelo nome.
Emerson, na criação do label faço a nomeação.
Essa é a procedure que estou usando em meus testes,
No evento PedidosClick preciso pegar o valor do label "L4", e armazenar.
procedure TfrmDelivery.btn_pedidoClick(Sender: TObject); var I, X: Integer; L1, L2, L3, L4, L5: TLabel; E: TCategoryPanel; begin X := 1; while not Dm_Api_pedidos.FDQueryGridPedidos.Eof do begin E := TCategoryPanel.Create(CategoryPanelGroup2); begin E.Name := 'CadPed' + IntToStr(X); E.Caption := 'Pedido'; E.Width := 303; E.Height := 105; E.Color := clInactiveBorder; TabOrder := I; E.PanelGroup := CategoryPanelGroup2; E.OnDblClick := PedidosClick; E.Tag := X; L1 := TLabel.Create(E); begin L1.Parent := E; L1.Left := 10; L1.Top := 8; L1.AlignWithMargins := True; L1.Caption := 'Pedido: '; L1.Name := 'LB_PEDIDO' + IntToStr(X); end; L2 := TLabel.Create(E); begin L2.Parent := E; L2.Top := 31; L2.Left := 10; L2.Width := 121; L2.Height := 17; L2.AutoSize := True; L2.AlignWithMargins := True; L2.Caption := 'Cliente: '; end; L5 := TLabel.Create(E); begin L5.Parent := E; L5.Top := 31; L5.Left := 67; // L2.Width := 121; // L2.Height := 17; L5.AutoSize := True; L5.AlignWithMargins := True; L5.Caption := Dm_Api_pedidos.FDQueryGridPedidos.FieldByName ('CLIENTE').AsString; end; L3 := TLabel.Create(E); begin L3.Parent := E; L3.Left := 146; L3.Top := 8; // L3.Width := 96; // L3.Height := 17; L3.AlignWithMargins := True; L3.Caption := Dm_Api_pedidos.FDQueryGridPedidos.FieldByName ('DESCR_STATUS').AsString; end; L4 := TLabel.Create(E); begin L4.Parent := E; L4.Left := 59; L4.Top := 8; L4.Name := 'LB_NRO_PD' + IntToStr(X); L4.Caption := IntToStr(Dm_Api_pedidos.FDQueryGridPedidos.FieldByName ('ID_PEDIDO').AsInteger); // L3.Width := 96; // L3.Height := 17; // L3.Caption := 'Status Pendente'; L4.AlignWithMargins := True; L4.Tag := X; end; end; Inc(X); Dm_Api_pedidos.FDQueryGridPedidos.Next; end; end;
23/02/2021
Bruno Divino
if label1.Tag = 0 then
ShowMessage('é tag 0')
else
ShowMessage('tag <> de 0');
Fala Gxf beleza?
Então, estou com um pouco de dificuldade, já tentei pegar o valor pela tag, porém sempre pego o último valor, o problema é.
Quando eu clicar duas vezes no CategoryPanel1, ou 2 etc.. preciso pegar o Caption do L4.
23/02/2021
Emerson Nascimento
var lblPedido: TLabel; cSufLabel: string; begin cSufLabel := Copy(Sender.Name,7,50); // obtém o conteúdo depois de 'CadPed' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_PEDIDO'+cSufLabel); // se o label foi criado no form // lblPedido := FindComponent('LB_PEDIDO'+cSufLabel); if assigned(lblPedido) then ShowMessage( TLabel(lblPedido).Caption ); end;
23/02/2021
Emerson Nascimento
var lblPedido: TComponent; cSufLabel: string; begin cSufLabel := Copy(Sender.Name,7,50); // obtém o conteúdo depois de 'CadPed' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_PEDIDO'+cSufLabel); // se o label foi criado no form // lblPedido := FindComponent('LB_PEDIDO'+cSufLabel); if assigned(lblPedido) then ShowMessage( TLabel(lblPedido).Caption ); end;
23/02/2021
Bruno Divino
var lblPedido: TComponent; cSufLabel: string; begin cSufLabel := Copy(Sender.Name,7,50); // obtém o conteúdo depois de ''CadPed'' {Aqui substitui o Sender.Name por E.Name que é o name do panel a ser pego é isso?} // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent(''LB_PEDIDO''+cSufLabel); // se o label foi criado no form // lblPedido := FindComponent(''LB_PEDIDO''+cSufLabel); if assigned(lblPedido) then ShowMessage( TLabel(lblPedido).Caption ); end;
Emerson tentei aqui porém não obtive exito.
Quando rodo a rotina de criação dos Panel, é criado dois panel com seus respectivos label, eu estou tentando capturar o valor do label " L4.Caption := IntToStr(Dm_Api_pedidos.FDQueryGridPedidos.FieldByName
(''ID_PEDIDO'').AsInteger);"
Os Label são criados no Parent do Panel, será que esse é o problema?
Eu poderia simplesmentes dar um ShowMessage(L4.Caption); porém isso me retorna o último valor gerado em "run time".
Preciso que quando seja clicando duas o evento traga o número do pedido que está no CategoryPanel que cliquei.
23/02/2021
Emerson Nascimento
publique a rotina (completa) onde quer utilizar o recurso.
23/02/2021
Bruno Divino
publique a rotina (completa) onde quer utilizar o recurso.
procedure TfrmDelivery.btn_pedidoClick(Sender: TObject); var I, X: Integer; L1, L2, L3, L5: TLabel; begin X := 1; while not Dm_Api_pedidos.FDQueryGridPedidos.Eof do begin E := TCategoryPanel.Create(CategoryPanelGroup2); begin E.Name := 'CadPed' + IntToStr(X); E.Caption := 'Pedido'; E.Width := 303; E.Height := 105; E.Color := clInactiveBorder; TabOrder := I; E.PanelGroup := CategoryPanelGroup2; E.OnDblClick := PedidosClick; E.Tag := X; L1 := TLabel.Create(E); begin L1.Parent := E; L1.Left := 10; L1.Top := 8; L1.AlignWithMargins := True; L1.Caption := 'Pedido: '; L1.Name := 'LB_PEDIDO' + IntToStr(X); end; L2 := TLabel.Create(E); begin L2.Parent := E; L2.Top := 31; L2.Left := 10; L2.Width := 121; L2.Height := 17; L2.AutoSize := True; L2.AlignWithMargins := True; L2.Caption := 'Cliente: '; end; L5 := TLabel.Create(E); begin L5.Parent := E; L5.Top := 31; L5.Left := 67; L5.Font.Name := 'Segoe UI'; // L2.Width := 121; // L2.Height := 17; L5.AutoSize := True; L5.AlignWithMargins := True; L5.Caption := Dm_Api_pedidos.FDQueryGridPedidos.FieldByName ('CLIENTE').AsString; end; L3 := TLabel.Create(E); begin L3.Parent := E; L3.Left := 146; L3.Top := 8; L3.Font.Name := 'Segoe UI'; // L3.Width := 96; // L3.Height := 17; L3.AlignWithMargins := True; L3.Caption := Dm_Api_pedidos.FDQueryGridPedidos.FieldByName ('DESCR_STATUS').AsString; end; L4 := TLabel.Create(E); begin L4.Parent := E; L4.Left := 59; L4.Top := 8; L4.Font.Name := 'Segoe UI'; L4.Name := 'LB_NRO_PD' + IntToStr(X); L4.Caption := IntToStr(Dm_Api_pedidos.FDQueryGridPedidos.FieldByName ('ID_PEDIDO').AsInteger); // L3.Width := 96; // L3.Height := 17; // L3.Caption := 'Status Pendente'; L4.AlignWithMargins := True; L4.Tag := X; end; end; Inc(X); Dm_Api_pedidos.FDQueryGridPedidos.Next; end; end;
Essa função é onde eu gero os campos de teste, é um speedbuttom dentro desse laço while que irá ler os dados da tabela e gerar dois TCategoryPanel com os labels. que gera os panel e os label.
Repare que essa função cria o evento "E.OnDblClick := PedidosClick;"
procedure TfrmDelivery.PedidosClick(Sender: TObject); var lblPedido: TComponent; cSufLabel: string; begin // aqui preciso pegar o valor do label(que é alimentado pela query no momento da criação) do seus respectivo TCategoryPanel. end;
Eu até consegui pegar com a função que você me passou, porém apenas do último registro criado, e não do primeiro, meu problema está em setar o TCategoryPanel correto na hora de usar a função PedidosClick.
23/02/2021
Emerson Nascimento
procedure TfrmDelivery.PedidosClick(Sender: TObject); var lblPedido: TComponent; cSufLabel: string; begin // aqui preciso pegar o valor do label(que é alimentado pela query no momento da criação) do seus respectivo TCategoryPanel. // assim será possível identificar o 'número' dos componentes, pelo sufixo cSufLabel := Trim(Copy(Sender.Name,7,50)); // obtém o conteúdo depois de 'CadPed' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_NRO_PD'+cSufLabel); // se não der certo com este // se o label foi criado no form // lblPedido := FindComponent('LB_NRO_PD'+cSufLabel); // tente com este if assigned(lblPedido) then ShowMessage( 'LB_PEDIDO'+cSufLabel+': ' + TLabel(lblPedido).Caption ); end;
23/02/2021
Bruno Divino
procedure TfrmDelivery.PedidosClick(Sender: TObject); var lblPedido: TComponent; cSufLabel: string; begin // aqui preciso pegar o valor do label(que é alimentado pela query no momento da criação) do seus respectivo TCategoryPanel. // assim será possível identificar o 'número' dos componentes, pelo sufixo cSufLabel := Trim(Copy(Sender.Name,7,50)); // obtém o conteúdo depois de 'CadPed' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_NRO_PD'+cSufLabel); // se não der certo com este // se o label foi criado no form // lblPedido := FindComponent('LB_NRO_PD'+cSufLabel); // tente com este if assigned(lblPedido) then ShowMessage( 'LB_PEDIDO'+cSufLabel+': ' + TLabel(lblPedido).Caption ); end;
procedure TfrmDelivery.PedidosClick(Sender: TObject); var lblPedido: TComponent; cSufLabel: string; begin // aqui preciso pegar o valor do label(que é alimentado pela query no momento da criação) do seus respectivo TCategoryPanel. // assim será possível identificar o 'número' dos componentes, pelo sufixo cSufLabel := Trim(Copy(Sender.Name,7,50)); // obtém o conteúdo depois de 'CadPed' // TObject does not contain a member named 'Name' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_NRO_PD'+cSufLabel); // se não der certo com este // se o label foi criado no form // lblPedido := FindComponent('LB_NRO_PD'+cSufLabel); // tente com este if assigned(lblPedido) then ShowMessage( 'LB_PEDIDO'+cSufLabel+': ' + TLabel(lblPedido).Caption ); end;
Emerson ao compilar tive o erro TObject does not contain a member named 'Name' la linha "SufLabel := Trim(Copy(Sender.Name,7,50));"
Aqui preciso trocar o parametro Sender?
Consegui compilar colocando no lugar do Sender E.name.
Porém só pego o valor do label 2, o label 1 não acontece nada.
23/02/2021
Bruno Divino
var lblPedido: TComponent; cSufLabel: string; begin cSufLabel := Copy(TWinControl(Sender).Name,7,50); // obtém o conteúdo depois de 'CadPed' // se o label foi criado no panel lblPedido := TWinControl(Sender).FindComponent('LB_NRO_PD'+cSufLabel); if assigned(lblPedido) then ShowMessage( TLabel(lblPedido).Caption ); end;
Muito obrigado Emerson, valores pegando corretamente.
Clique aqui para fazer login e interagir na Comunidade :)