Dynamic component name Delphi

Asked

Viewed 502 times

0

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

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

which 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;
  • I couldn’t understand why I asked the database. The code does not use any query data. To know why the code is not working you need to know how the Tpanel were created and named. Findcomponent will return the reference to the Panel only if it is the Owner of the Panel, and if Panel.Name is correctly specified.

  • Ricardo, Panel are created at design time and are invisible. According to the query they will be visible or not.

  • Has each Tpanel been named in the Object Inspector (Pn1, Pn2, ...)? How many Panels are in the form?

  • Yes. In Object Inspector. There are several but the ones I need to handle are 7. Pn1... Pn7

  • Ok. And what you read from the query to determine which panel will be enabled?

  • Actually each loop will activate the corresponding Panel. Ex. var pointer = 1 Pn1 is visible. I will use the data from the fields for popular Label’s that have inside the Panel.

  • I tried to chat, but I couldn’t. I did a test like this and it worked:Project Tform2.Button1click(Sender: Tobject); var panel: Tpanel; pointer: integer; Begin for pointer := 1 to 5 of Begin panel := Findcomponent('Pn' + Inttostr(pointer)) as Tpanel; if Assigned() then panel.Visible := True; end; end;

  • As soon as I get home I’ll test it and come back here

Show 4 more comments

2 answers

1

So you can catch 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;
  • Gave an Exception

  • Are you testing? Is it to check the code? If the element you want to pick up is a Tpanel I believe will work, for you to do the tests more effectively, put a string manually to catch only 1 element, so you already rule out any other problem that might happen in while.

  • Exactly is Tpanel, I am testing to disable and even manually putting the error.

  • Qual é a Exception?

  • Exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'.

  • I made a change in the reply using dashboard, see if it will work this way.

  • Now gives no exception but does not make invisible the Panel

  • If you swap not panel. Visible; for False, return the exception?

  • changed and did not return the exception.

  • But after the change, it worked?

  • Did not disable the Panel and did not give the exception

  • It is not entering the 'if panel <> nil then', therefore it is not finding the element, and the panel variable is null, check if the name is actually 'Fn' something from the panel.

  • The names are Pn1, Pn2, ... Pn7 . I was looking here, they are inside another panel, will have something to do?

  • I believe that it won’t make a difference being in another panel, because Findcomponent simply picks up the element using a string, I’ve seen this way of also using Tpanel(Findcomponent('Pn' + Inttostr(pointer)), but when I saw the person creating the element, I don’t understand why he can’t catch it, the Findcomponent is part of the Tcomponent that has probably already been imported, since you use other elements.

  • I updated the question with the code I have so far.

  • Just try one more thing, I was reading here tries this way: painExterno.Findcomponent(...

  • panel := Panel2.Findcomponent('Pn' + Inttostr(pointer)) as Tpanel; nothing done

  • How is this panel created? Ex: panel := Tpanel.Create(Self) / Tpanel.Create(nil) / Tpanel.Create(Application) ???

  • It is inserted in design time. And will be visible dynamically

Show 14 more comments

0

I did 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 (different from the caption property).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.