1. Technology

MDI Child Count By Class

Count MDI Children By Child Class Name in Delphi MDI Applications

By

Delphi MDI Child Count By Class Name

Delphi MDI Child Count By Class Name

An MDI application typically displays more than one document or child window within a single parent window.

A user ("Commodianus") of the About Delphi Programming Forum posted a question Count MDI Children of Class: I have two types of MDI Child forms in my application. How might I go about counting each one? One is TFrmStatus the Other is FrmChannel. How do I know how many TFrmStatus children are alive?.

MDI Child Count By Class

Here's how I do it in my Delphi MDI application...

First a "setup" I use for every MDI application:

0. Download Sample Delphi MDI Project Skeleton.

1. There's a base class for all MDI child forms (TChildBaseForm). TChildBaseForm's FormStyle property is set to "fsMDIChild.". Here's the skeleton code:


unit ChildBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms, Dialogs;

type
  //base class for all MDI child forms
  TChildBaseForm = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction) ;
  end;

implementation
{$R *.dfm}

procedure TChildBaseForm.FormClose(Sender: TObject; var Action: TCloseAction) ;
begin
  Action := caFree;
end;

end.
Note that the OnFormClose event is used to set the Action parameter to caFree to properly free the memory occupied by the child form when it closes. If this is left out, the form would only minimize.

Note also that forms of this class are never instantiated - TChildBaseForm is only used as a base class in visual form inheritance.

2. Every child form that will actually be created inherits from the TChildBaseForm. Here are two such child forms:


unit ChildImage;
...
type
  TChildImageForm = class(TChildBaseForm)

-----------------
unit ChildTree;

...
type
  TChildTreeForm = class(TChildBaseForm)

3. Finally, there's a CreateMDIChild function in the MDI main form's unit that is called when a child needs to be created:


function TMainForm.CreateMDIChild(const baseChild : TChildBaseFormClass) : TChildBaseForm;

  function CountOf(const childClassName : string) : integer;
  var
    cnt : integer;
  begin
    result := 0;
    for cnt := 0 to -1 + MDIChildCount do
    begin
      if MDIChildren[cnt].ClassName = childClassName then
        result := result + 1;
    end;
  end;
begin
  result := baseChild.Create(Application) ;
  result.Caption := Format('%s %d', [baseChild.ClassName, CountOf(baseChild.ClassName)]) ;
end;
The TChildBaseFormClass is defined as:

type
  TChildBaseFormClass = class of TChildBaseForm;
To actually create an MDI child the next code is used:

//to create a "child image" form
CreateMDIChild(TChildImageForm) ;

...

//to create a "child tree" form
CreateMDIChild(TChildTreeForm) ;
Note the CountOf(const childClassName : string) sub-function in the CreateMDIChild function. CountOf returns the number of child forms by their class.

I hope this answers the question posted on the Forum :)

Download Sample Project.

Delphi tips navigator:
» Implementing the On-AFTER-Create And On-BEFORE-Create Events for Delphi Forms
« Validate an XML Against a DTD Using TXMLDocument and Delphi

  1. About.com
  2. Technology
  3. Delphi
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2009 Tips
  7. MDI Child Count By Class - Count MDI Children By Child Class Name in Delphi MDI Applications

©2014 About.com. All rights reserved.