whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

19 Visitors Online


 
...know whether a form already exist before you dynamically create it?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (26):  
     


{
  Q: How to know whether a form already exist before I dynamically create it ?

  A: See the Forms and FormCount property of TScreen. You can iterate
     through the forms, and test to see if your form is there.
}

function IsFormOpen(const FormName : string): Boolean;
var
  
i: Integer;
begin
  
Result := False;
  for i := Screen.FormCount - 1 DownTo do
    if 
(Screen.Forms[i].Name = FormName) then
    begin
      
Result := True;
      Break;
    end;
end;

// Example: Showing a TForm.
// First check, if the Form (here Form2) is open. If not, create it.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not 
IsFormOpen('Form2') then
    
Form2 := TForm2.Create(Self);

  Form2.Show
end;

{ For MDI Children }

function IsMDIChildOpen(const AFormName: TForm; const AMDIChildName : string): Boolean;
var
  
i: Integer;
begin
  
Result := False;
  for i := Pred(AFormName.MDIChildCount) DownTo do
    if 
(AFormName.MDIChildren[i].Name = AMDIChildName) then
    begin
      
Result := True;
      Break;
    end;
end;

// Example: Showing a MDI Child.
// First check, if the MDI Child is open. If not, create it.

procedure TForm1.Button2Click(Sender: TObject);
begin
   if not 
IsMDIChildOpen(Form1, 'MyMDIChild') then
    
MyMDIChild := TMyMDIChild.Create(Self);

  MyMDIChild.Show;
  MyMDIChild.BringToFront;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners