Mlody87
Mlody87

Reputation: 435

Delphi, find form by name

how can I find form by name? On this form I have Edit (TEdit) and i would like to write something in this TEdit (its name e.g.: adress) but I have only form name. Can you help me?

Upvotes: 2

Views: 11749

Answers (3)

Free Consulting
Free Consulting

Reputation: 4402

There is a simpler way of finding a form by name. Since all of auto-created form objects become owned by Application object and TApplication inherits from TComponent, you can either iterate thru Application.Components array property or use Application.FindComponent method.

var 
  Form: TForm;
begin
  Form := Application.FindComponent('LostForm1') as TForm;
  if Assigned(Form) then
    Form.Show
  else
    { error, can't find it } 

Note that FindComponent is case-insensitive.

Upvotes: 9

Larsdk
Larsdk

Reputation: 713

This answer assumes you are making a VCL application. I don't know if FireMonkey has a similar solution.

All forms are added to the global Screen (declared in Vcl.Forms) object when they are created. Thus you can make a little helper function like this

function FindFormByName(const AName: string): TForm;
var
  i: Integer;
begin
  for i := 0 to Screen.FormCount - 1 do
  begin
    Result := Screen.Forms[i];
    if (Result.Name = AName) then
      Exit;
  end;
  Result := nil;
end;

Upvotes: 6

Ross Bush
Ross Bush

Reputation: 15175

You can use the FindWindow function if you know the form title or the class name of the form.

Upvotes: 0

Related Questions