Solved

Form instance!

Posted on 2002-03-26
3
214 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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