Solved

Form instance!

Posted on 2002-03-26
3
221 Views
Last Modified: 2012-05-04
This is a easy one for you experts,

Let's say I have 3 forms on my project, they all look like the same, and their functionality to the end are quite similar.
The main difference between them is that they access diferent databases and have some different form captions.

I want to be able to create only one form at design time and then instanciate the forms as MDIChilds at runtime so that to the end user he will think that there are 3 different forms running.

I want to open different instances of the form at the same time, and want to be able to track if the especific instance is already open.

Hope you guys can help me!
Thanks,
VSF
www.victory.hpg.com.br
0
Comment
Question by:VSF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 30 total points
ID: 6896258
I am just going to share some code. I'm not going to add any comments... Have fun trying to find out what's happening here... ;-)

unit Unit1;

interface

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

type
  TMDIForm = class(TForm)
    MainMenu1: TMainMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    procedure N11Click(Sender: TObject);
    procedure N21Click(Sender: TObject);
    procedure N31Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MDIForm: TMDIForm;

implementation

uses Unit2;

{$R *.DFM}

function OpenMDIForm(FormClass: TFormClass; ATag: Integer): TForm;
var
  I: Integer;
  Found: Boolean;
begin
  Screen.Cursor := crHourGlass;
  with Application.MainForm do try
    I := MDIChildCount;
    repeat
      Dec(I);
      Found := (MDIChildren[I] is FormClass) and (MDIChildren[I].Tag = ATag);
    until Found or (I <= 0);
    if Found then begin
      Result := MDIChildren[I];
    end
    else begin
      Result := FormClass.Create(Application.MainForm);
      Result.Font.Assign(Application.MainForm.Font);
    end;
    if (Result.WindowState = wsMinimized) then ShowWindow(Result.Handle, SW_RESTORE);
    Result.Show;
    Result.Tag := ATag;
  except
    Result := nil;
  end;
  Screen.Cursor := crDefault;
  Application.ProcessMessages;
end;

function FindMDIForm(FormClass: TFormClass; ATag: Integer): TForm;
var
  I: Integer;
begin
  Result := nil;
  with Application.MainForm do begin
    try
      I := MDIChildCount;
      repeat
        Dec(I);
        if (MDIChildren[I] is FormClass) and (MDIChildren[I].Tag = ATag) and not (csDestroying in MDIChildren[I].ComponentState) then
          Result := MDIChildren[I];
      until Assigned(Result) or (I <= 0);
    except
      Result := nil;
    end;
  end;
  Application.ProcessMessages;
end;

procedure CloseMDIForm(FormClass: TFormClass; ATag: Integer);
var
  I: Integer;
begin
  with Application.MainForm do try
    I := MDIChildCount - 1;
    repeat
      if (MDIChildren[I] is FormClass) and (MDIChildren[I].Tag = ATag) then
        MDIChildren[I].Close;
      Dec(I);
    until (I < 0);
  finally
    Application.ProcessMessages;
  end;
end;

procedure TMDIForm.N11Click(Sender: TObject);
begin
  OpenMDIForm(TMDIChild, 1).Caption := 'This is form 1.';
end;

procedure TMDIForm.N21Click(Sender: TObject);
begin
  if assigned(FindMDIForm(TMDIChild, 1)) then begin
    OpenMDIForm(TMDIChild, 2).Caption := 'This is the second form.';
  end
  else begin
    ShowMessage('You have to open MDI 1 first.');
  end;
end;

procedure TMDIForm.N31Click(Sender: TObject);
begin
  CloseMDIForm(TMDIChild, 2);
  OpenMDIForm(TMDIChild, 3).Caption := 'This form closes form 2.';;
end;

end.
0
 
LVL 4

Expert Comment

by:Radler
ID: 6896259
Hi VSF,

Saw your email and my conclusione is that you is Brazilian, Rigth!?

See this

procedure ShowSpecific( ACaption, DatabaseDir : string );
var
   Dlg : TMyDialog;
begin
   Aplication.CreateForm( TMyDialog, Dlg );
   try
       Dlg.Database1.Directory:=DatabaseDir;
       Dlg.Caption:=ACaption;
       Dlg.Table1.Open();
       Dlg.ShowModal();  //Replace to show and see *
   finally
       Dlg.Free();   //To modal form only
   end;
end;

* To a MDI Form erase the Dlg.Free and assign a Action:=caFree into the onClose event.

T++, Radler;

Feliz Páscoa.
0
 
LVL 3

Author Comment

by:VSF
ID: 6933874
This should do it!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question