Solved

Form instance!

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now