Dynamic component name Delphi

0

In jQuery we can reference an element of the form below:

var tipo = "B";
$("#campoTipo"+tipo).val("Teste");

that would be the same as:

$("#campoTipoB").val("Teste");

I need in Delphi, disable some buttons that depend on a Query

procedure TFormCupom.BitBtn3Click(Sender: TObject);
var
ponteiro: Integer;
painel: TPanel;  
begin
 DM.FDConexao.Connected := true;
 ponteiro := 0;
 with DM.FDQ_Recentes do
 begin
  close;
  sql.clear;
  sql.add('SELECT * FROM rotativo');
  open;
  First; // primeiro
  while not eof do
   begin
   inc(ponteiro);
   painel := FindComponent('Pn' + IntToStr(ponteiro)) as TPanel;
    if painel <> nil then
      painel.Visible := false;
      Next; // proximo
    end;
   end;
end;
    
asked by anonymous 24.09.2017 / 01:53

2 answers

1

So you can get the element using a string, as in jquery:

var 
painel: TPanel;
painel := FindComponent('Pn' + IntToStr(ponteiro)) as TPanel;
if painel <> nil then  
    painel.Visible := not painel.Visible;
    
24.09.2017 / 02:15
0

I ran a test with this code and it worked: procedure TForm2.Button1Click(Sender: TObject); var panel: TPanel; ponteiro: integer; begin for ponteiro := 1 to 5 do begin panel := FindComponent('Pn' + IntToStr(ponteiro)) as TPanel; if Assigned(panel) then panel.Visible := True; end; end; A possible problem would be if the panels did not have the name property set as expected (other than the caption property).

    
28.09.2017 / 21:59