Find Delphi component by string

Asked

Viewed 688 times

2

I need to open a form on Delphi, but instead of calling him straight by name UniForm1, I need to call you for the value stored in a String.

procedure TMainForm.UniTreeView1Change(Sender: TObject; Node: TUniTreeNode);
var nome : string;
    tela : TUniForm;
begin
   nome := Node.Text;
   tela := FindComponent(nome) as TUniForm;

   //UniPanel1.Caption := nome;

   tela.Parent := UniPanel1;
   tela.Show;
   tela.SetFocus;
end;

However, when I select an item on Treeview, the following error occurs and does not open the form:

Project Log_project.exe Raised Exception class $C0000005 with message 'access Violation at 0x006d0f13: read of Adress 0x000003ce'.

  • What are you trying to do? The Findcomponent function is in the form (Tmainform). The form you want to search for is almost certainly not in that list. Because you don’t have a Dictionary with the Forms and their string names?

1 answer

3


As @Tiago Rodrigues said, form that you’re looking for probably isn’t inside that other form. Within Application you have that same function FindComponent, can use it to find your form.

tela := Application.FindComponent(nome) as TUniForm;

At this point you’re not sure if the form that you are looking for was found, so ideally you check if it really was found before doing anything with it.

if (tela <> nil) then
  begin
    tela.Parent := UniPanel1;
    tela.Show;
    if (tela.Showing) then
      tela.SetFocus;
  end;

Just one more thing before I SetFocus on any component, make sure it is displayed with the property Showing. If this property returns True, means that the component is being displayed, if it returns False, the component is not being displayed. This verification is necessary as if the component is not being displayed the program will launch an exception. When the component is not a form, I also check if the Enabled of him also not this False, to give a SetFocus.

  • This time, no error, but variable screen is always getting nil. So the form is never being found? What should I do?

  • How do you create this form that you are looking for?

  • It was created manually even during the development of the program. Now what I’m trying to do is display the name of all Forms in a Treeview, so when you select a name, open it in the next panel.

  • But the forms that you want to list are instantiated? procedure in a project of mine containing several forms created and instantiated as soon as he starts the project and he showed me all: var&#xA; i: integer;&#xA; Forms: String;&#xA;begin&#xA; Forms := '';&#xA; for i := 0 to Application.ComponentCount - 1 do&#xA; if (Application.Components[i] is Tform) then&#xA; Forms := Forms + TForm(Application.Components[i]).Name;&#xA; ShowMessage(Forms);

  • I copied the code and after running it, Showmessage showed only "Serverform". What does that mean?

  • It means the others forms are not children of Application or they were not instantiated. What version of your Delphi?

  • Delphi XE 10 Seattle

  • So I need to do what to fix this?

  • Goes on the menu Project and click on Options, select the session Forms, and check whether your Forms are in Auto-create forms or in Available forms.

  • Are all in Available Forms.

  • Then your form does not exist when you search. For this reason you do not find. Before doing this search you need to instantiate your form.

  • How do I do it?

  • The easiest way out is for you to pass the forms you want to find in this scan of Available forms for Auto-create forms.

  • It did not work... [dcc32 Error] Log_project.dpr(20): E2197 Constant Object cannot be passed as var Parameter

Show 9 more comments

Browser other questions tagged

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