Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Manage MDI forms Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Sep-03
Category
Others
Language
Delphi 5.x
Views
103
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Erwin Molendijk

Manage MDI forms. Each form will be visble only once. Form will be created if 
needed.

Answer:

1   {
2   
3   This article describes a base class for your mainform.
4   (You can inherite from this form or use it as is)
5   
6   Three new routines are presented. They will enable you to easily
7   manage MDI forms:
8   - Only one instance per MDI Class will be activated
9   - Creation of MDI class will be handled by routines if needed
10  - Activated MDI class will be focused if needed
11  
12  }
13  
14  type
15    TMDIClass = class of TForm;
16  
17  type
18    TBaseMainForm = class(TForm)
19      {... }
20    public
21      { Public declarations }
22      function ActivateMDIClass(MDIClass: TMDIClass): TForm;
23      function GetMDIClassIndex(MDIClass: TMDIClass): Integer;
24      function MDIClassIsActive(MDIClass: TMDIClass): Boolean;
25      {  ... }
26    end;
27  
28  implementation
29  
30  {
31  Use ActivateMDIClass() to activate a mdi child class.
32  If the class is not created yet, it will be.
33  The mdi child will be shown on screen and focused.
34  }
35  
36  function TBaseMainForm.ActivateMDIClass(MDIClass: TMDIClass): TForm;
37  var
38    i: Integer;
39  
40  begin
41    // Try to find index of MDIClass form in MDI child list
42    i := GetMDIClassIndex(MDIClass);
43  
44    // if index is not found (-1) then create the form
45    if i = -1 then
46      Result := MDIClass.Create(Application)
47    else
48      Result := MDIChildren[i];
49  
50    // bring it to front
51    Result.Show;
52    Result.BringToFront;
53  end;
54  
55  {
56    Get mdi child index of specified MDIClass.
57    Returns -1 if the MDIClass does not exist as a created MDI form
58  }
59  
60  function TBaseMainForm.GetMDIClassIndex(
61    MDIClass: TMDIClass): Integer;
62  var
63    i: Integer;
64  begin
65    // Default index  -1 =  MDIClass not found
66    Result := -1;
67  
68    // try to find a MDI child of correct MDIClass class
69    for i := 0 to MDIChildCount - 1 do
70      if MDIChildren[i].ClassType = MDIClass then
71        Result := i;
72  end;
73  
74  {
75    Returns true is the MDIClass exists as a created MDI form
76  }
77  
78  function TBaseMainForm.MDIClassIsActive(
79    MDIClass: TMDIClass): Boolean;
80  begin
81    Result := GetMDIClassIndex(MDIClass) <> -1;
82  end;


Usage Example 

Create a mainform, inherited from TBaseMainForm. 

Create two mdi forms called TfrmBrainstorm and TfrmReport. 
Make sure ...FormStyle=fsMDIChild. 
Make sure MDI childs can be closed: 

83  procedure...FormClose(Sender: TObject;
84    var Action: TCloseAction);
85  begin
86    Action := caFree;
87  end;
88  
89  Now use the following code to activate those mdi forms: 
90  
91  procedure TMainForm.OnClick1(Sender: TObject);
92  begin
93    ActivateMDIClass(TfrmBrainstorm);
94  end;
95  
96  procedure TMainForm.OnClick2(Sender: TObject);
97  begin
98    ActivateMDIClass(TfrmReport);
99  end;



			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC