Advertisement

04.09.2008 at 02:26PM PDT, ID: 23309818
[x]
Attachment Details

How to create MDI child forms at runtime and a button for each of them in the parent form

Asked by fedra in Delphi Programming, Delphi Programming Language, Delphi Components

Tags: Delphi 7, creat MDI child forms at runtime and a button for each of them

Hi!

Do you know the WindowMenu property of a (parent) form?
It links to a menu that list all current MDI children of an application.

I want to list all existing children also in other place. More exactly I want to create an interface similar with Macromedia Dreamweaver or Delphi 7, where each MDI child has its own tab from where it can be quickly accessed.

I started the work from TForm.Notification but something is wrong.
The idea was to create a new tab in a TPageControl every time a form was created at runtime.
Somebody came with idea of adding a routine in each MDI form (in Create and Destroy) but I don't like the idea because I have many different forms derived from TFrom in my application.

The code that I tried is:


VAR  
    Initializing: Boolean= True;
    FormID: Integer= 0;

procedure TfrmMain.Button1Click(Sender: TObject);
VAR F: TForm;
begin
 F:= TForm.Create(Self);
 F.Caption:= 'xx';
 F.Name:= 'Form'+ i2s(FormID);
 F.Tag:= FormID;  
 F.FormStyle:= fsMDIChild;
 F.Show;
 F.BringToFront;
end;


procedure TfrmMain.FormCreate(Sender: TObject);
begin
   Initializing:= FALSE;
end;


procedure TfrmMain.Notification(AComponent: TComponent; Operation: TOperation);
VAR btn: TButton;
begin
 inherited;
 if NOT Initializing
 AND (Operation= opInsert)
 AND (AComponent is TForm)
 then
  begin
   inc(FormID);
     
   btn:= TButton.Create(Self);
   btn.Parent:= Self;
   btn.Visible:= TRUE;
   btn.BringToFront;
   btn.Top := 1;
   btn.Caption:=  (AComponent as TForm).Caption;    <--- AV here
   btn.Tag:= FormID;
   AComponent.Tag:= FormID;
  end;

if  (Operation= opRemove) then RemoveAsocButton;
end;


The code creates a MDI child when you push the button.
At this moment the Notification is aware that a new component is ready to be created. Here I want to create the list of existing MDI children (the above code tries to create a button for each of these MDI children). However it fails when at this line (AComponent as TForm).Caption.
The AComponent seems to be un-initialized.

PS: I also need the routine that deletes the corresponding button when its MDI child is closed.
PS: I accept also other approaches as long as they are elegant (for example if I dont need to insert code in all my derived forms). Maybe to create a MDI child and set a tab (TTabSheet) as its parent.
Start Free Trial
[+][-]04.09.2008 at 07:21PM PDT, ID: 21321172

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Delphi Programming, Delphi Programming Language, Delphi Components
Tags: Delphi 7, creat MDI child forms at runtime and a button for each of them
Sign Up Now!
Solution Provided By: TheRealLoki
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.10.2008 at 12:17AM PDT, ID: 21322386

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.10.2008 at 12:42AM PDT, ID: 21322505

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.10.2008 at 05:15AM PDT, ID: 21323849

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.10.2008 at 10:15AM PDT, ID: 21327100

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628