whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews ¦  misc ¦  forum
 kylix ¦  tutorials ¦  online shop ¦  photos ¦  Add&Win Game

Tips (1565)

Database (91)
Files (139)
Forms (113)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (133)
IntraWeb (0)
Kylix (10)
Math (77)
Misc (128)
Multimedia (46)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (246)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

51 Visitors Online


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

Tip Rating (4):  
     


{
  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