Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the form already open?

Tags:

delphi

I have been using the following code to check if a form already exists:

function FormExists(apForm: TObject): boolean;
var i: Word;
begin
  Result := False;
  for i := 0 to Application.ComponentCount-1 do
    if Application.Components[i] = apForm then begin
      Result := True;
      Break;
    end;
end;

I got it some years ago from a project I participated in. It was one of my first Delphi projects.

It works.

But this week I got wandering if there is a better, faster way to do this.

like image 393
Jlouro Avatar asked Aug 13 '12 23:08

Jlouro


People also ask

How do you check if a Windows Form is open or not?

Just call Show method on the instance. You can check Form. Visible property to check if the form open at the moment.

How check form is loaded or not in C#?

You can use Form. IsActive property.

How do I open a second form in first form?

There is a login from when I enter a password and username and then the page goes directly to another page named form2. Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2. Now click the submit button and write the code.

Do people still use Windows Forms?

This is also the type of application that small and medium enterprises/companies need. In addition, just using components like Telerik UI or DevExpress, WinForm can create modern, dreamlike, long-lasting interfaces. flowery flax. With these advantages, WinForm still lives well and lives well.


1 Answers

You can use Screen.Forms for that instead. It lessens the items you're iterating through:

function FormExists(apForm: TForm): boolean;
var 
  i: Word;
begin
  Result := False;
  for i := 0 to Screen.FormCount - 1 do
    if Screen.Forms[i] = apForm then 
    begin
      Result := True;
      Break;
    end;
end;

However, it's worth noting that if you have apForm already, you know it exists and there's no need to be searching for it.

like image 142
Ken White Avatar answered Oct 06 '22 06:10

Ken White