Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Form instance!

Posted on 2002-03-26
3
Medium Priority
?
222 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 120 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 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