Solved

Form instance!

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

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 89
When i run adoquery my application freezes 26 214
TEMBEDDEDWB how can i change its user agent ? 8 106
Firemonkey webbrowser scrollbars ? 1 73
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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