#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Limiting number of MDI children


    I have written an MDI application which is working well but I would like to limit the number of times a child window of any type can be displayed to one. That is, once a particular child window is displayed it cannot be displayed again until after it has been closed.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Augsburg Germany
    Posts
    12
    Rep Power
    0
    sounds like a simple if assigend(VarToKeep)) then xxx.Show else xxx:=Txxx.create(owner,@VarToKeep), which will need a Template which will set @VarToKeep) to nil on destroy.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    Do you want to limit the number of a particular form to just one occurrance, or do you want to limit the number of just one occurrence for the data it is displaying?

    As example,
    If your application opens a text editor form and a spreadsheet form, do you want to limit it to just one editor and just one spreadsheet.

    or

    do you want to limit it to just one occurrence of a text editor for a particular file, but can have multiple text editor forms open, and same for the spreadsheet form, limit it to just one occurrence for a particular file, but multiple speadsheet forms can be open?

    Both are possible, and you can accomplish this with a CLASS method.

    A class method of a form can be called even if the form does not exist, so basically, your going to ask the form to see if it exists.

    An example of the first option (just one occurrence without regard to what it is displaying)

    First declare the CLASS method in the public section of your form's declaration

    Code:
    unit My_Form_Unit;
    
    ...
    
    type
      TMy_Form = class(TForm)
        ...
      private
        ..
      public
        CLASS procedure Open_Singleton_Form;//notice the word CLASS before procedure
      end;
    then define the class procedure

    Code:
    class procedure TMy_Form.Open_Singleton_Form;
    var
       F: TMy_Form;
       N: Integer;
    begin
       F := nil;
       With Application.MainForm Do 
          for N := 0 to MDIChildCount - 1 do//search all forms to see if TMy_Form Exists
             if MDIChildren[N] is TMy_Form then
             begin
                F := MDIChildren[N] As TMy_Form;//Found an occurance; Assign to F.
                Break;
             end;
       if F = nil then
          TMy_Form.Create(Application)//if F is unassigned, then create the form
       else
          With F do//if F was assigned, bring the previous instance forward
          begin
             if WindowState = wsMinimized then
                WindowState := wsNormal;
             BringToFront;
          end;
    end;
    you now create the form, using this new class method
    Code:
    uses My_Form_Unit
    
    procedure XXX.XXX...
    begin
      TMy_Form.Open_Singleton_Form;//will create new or bring forward if it already exists
      ...
    end;
    if you want to limit as per the second option, then just having the form open is not the only test, you then want to determine if the same information is to be displayed...

    Code:
    Type
      TMy_Form = class(TForm)
        ..
      private
        FileName: String;
        ..
      public
        CLASS procedure Open_Singleton_Form;
      end;
    
    procedure TMy_Form.FormCreate(Sender: TObject);
    begin
       FileName := '<MyFileName>';
       ...
    end;
    
    class procedure TMy_Form.Open_Singleton_Form;//again, notice CLASS before procedure
    var
       F: TMy_Form;
       N: Integer;
    begin
       F := nil;
       With Application.MainForm Do 
          for N := 0 to MDIChildCount - 1 do//search all forms to see if TMyForm Exists
             if MDIChildren[N] is TMy_Form then
             begin
                F := MDIChildren[N] As TMy_Form;
                //you'll need some class instance variable to determine if it is what you are looking for - say filename...
                if F.FileName <> <FileName you are now attempting to open> then
                   F := nil //wasn't what we were looking for - Also FileName will need to be declared in your form's declaration.
                else
                   Break;
             end;
       if F = nil then
          TMy_Form.Create(Application)//if F is unassigned, then create the form
       else
          With F do//if F was assigned, bring the previous instance forward
          begin
             if WindowState = wsMinimized then
                WindowState := wsNormal;
             BringToFront;
          end;
    end;

IMN logo majestic logo threadwatch logo seochat tools logo