2
votes

I have two separate forms in my application. I created one additional empty form and placed a TMainMenu component on it with two menu items. This new form will serve as the main form from which everything else will be loaded. Now I want it to work like this: when you click either of the two menu items, it should load the respective form's contents the menu item associated with to the main form. How can I achieve this? Or what is the typical approach to this kind of problem? Please, provide a simple illustrative example.

enter image description hereenter image description here

3
Use TFrame, they are made specifically for that. - Kromster
I think this can be solved by a very basic tutorial. Check out this youtube video. - CsBalazsHungary
I looked into it. Thanks. But How do I convert my existing forms to frames? - Mikhail
Create a new TFrame, then copy/paste your existing TForm controls and associated code over to it. - Remy Lebeau

3 Answers

0
votes

Create a TPanel on your MainForm and set its Align := alClient, BevelOuter := bvNone. Also give the MainForm attributes Form1: TForm1 and Form2: TForm2. Then display Form1 or Form2 in this way:

Showing Form1:

if not Assigned(MainForm.Form1) then
  MainForm.Form1 := TForm1.Create(MainForm);
MainForm.Form1.Parent := MainForm.Panel1;
MainForm.Form1.Align := alClient;
MainForm.Form1.BorderStyle := bsNone;
MainForm.Form1.Visible := True;
if Assigned(MainForm.Form2) then
  MainForm.Form2.Visible := False;

Of course you don't need to write MainForm; it's just to make the example clear.

0
votes

If you have a bunch of Forms already created and want to add the ability of being show "docked" you could change the inheritance to a new template instead of TFrom.
By adding and overloaded constructor you will be able to use them as usual or "docked".
To change the inheritance you only have to replace

type
  TYourForm = class(TForm)

with

type
  TYourForm = class(TTemplate)

and replace the object in your DMF's with inherited

{
  public
    Constructor Create(AOwner:TComponent;AParent:TWinControl=nil);Overload;
.....
}
constructor TTemplate.Create(AOwner: TComponent; AParent: TWinControl);
begin
  inherited Create(AOwner);
  if Assigned(AParent) then
    begin
      BorderStyle := bsNone;
      Parent := AParent;
      Align := alClient;
    end;
end;
0
votes

if you want to embed other form in the your main form put a tpanel in to your main form set alclient property. And when clicked set fromxxx.parent is your panel name.

here is a sample code

  begin
    if Dm.TblUser.Active=False then 
      Dm.TblUser.Active := True;
    if FrmPUserG=nil then 
      FrmPUserG := TFrmPUserG.Create(Self);
    FrmpUserG.Parent := PnLContainer;
    FrmpUserG.Align := alClient;
    FrmpUserG.BorderStyle := bsNone;
    FrmpUserG.Visible := True;
    FrmpUserG.BringToFront;
  end;