MainMenu

eNarc
eNarc used Ask the Experts™
on
Hi, I've got a MainMenu1 on Form7

I've Got a Unit1 That Stores The Procedures and Functions.

Now I need to create the MainMenus and there Subitems within this Unit1.

and I also need the OnClick to Call from the Unit1 so everything is in the Unit1

the Making of the MainMenu Items and the Showing of the OnClick.

how is this done because all I'm getting is errors?






dfm of form
#####################################################
object Form7: TForm7
  Left = 0
  Top = 0
  Caption = 'Form7'
  ClientHeight = 216
  ClientWidth = 426
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  Menu = MainMenu1
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object MainMenu1: TMainMenu
    Left = 112
    Top = 56
  end
end
#####################################################

pas of form
#####################################################
unit Unit7;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;

type
  TForm7 = class(TForm)
    MainMenu1: TMainMenu;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form7: TForm7;

implementation
  uses unit1;
{$R *.dfm}

procedure TForm7.FormCreate(Sender: TObject);
begin
Make;
end;

end.
#####################################################

pas of unit
#####################################################
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;

  procedure Make;
implementation
  uses unit7;

procedure Show(Sender: TObject);
begin
showmessage('##Completed##');
end;

procedure Make;
var item: TMenuItem;
    item2: tmenuitem;
begin
  item := tmenuitem.create(Form7.MainMenu1);
  item.Caption := '&File';
  Form7.MainMenu1.Items.Add(item);

  item2 := tmenuitem.create(Form7.MainMenu1);
  item2.Caption := 'E&xit';
  item2.OnClick := Show;
  item.Add(item2);
end;

end.

#####################################################




Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There are a number of things that I might suggest doing differently. Nonetheless here is a corrected version of your code.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;

type
  TForm1 = class(TForm)
  MainMenu1: TMainMenu;
    procedure FormCreate(Sender: TObject);
  private
     Procedure Make;
     Procedure Show(Sender : TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
     Make;
end;

procedure TForm1.Make;
var item: TMenuItem;
    item2: tmenuitem;
begin
  item := tmenuitem.create(MainMenu1);
  item.Caption := '&File';
  MainMenu1.Items.Add(item);

  item2 := tmenuitem.create(MainMenu1);
  item2.Caption := 'E&xit';
  item2.OnClick := Show;
  item.Add(item2);
end;

procedure TForm1.Show(Sender: TObject);
begin
     ShowMessage('##Completed##');
end;

end

Open in new window

Here is a somewhat more useful example of the same concept
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;

type
  TForm1 = class(TForm)
  MainMenu1: TMainMenu;
    procedure FormCreate(Sender: TObject);
  private
     Procedure Make;
     Procedure Show(Sender : TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
     Make;
end;

procedure TForm1.Make;
var Item, SubItem: TMenuItem;
begin
  Item := TMenuItem.Create(MainMenu1);
  Item.Caption := '&File';
  MainMenu1.Items.Add(item);

  SubItem := TMenuItem.Create(MainMenu1);
  SubItem.Caption := '&Open';
  SubItem.OnClick := Show;
  Item.Add(SubItem);

  SubItem := TMenuItem.Create(MainMenu1);
  SubItem.Caption := 'E&xit';
  SubItem.OnClick := Show;
  Item.Add(SubItem);
end;

procedure TForm1.Show(Sender: TObject);
begin
     if Sender is TMenuItem then
          ShowMessage(TMenuItem(Sender).Caption + ' Clicked');
end;

end.

Open in new window

Author

Commented:
Hi SteveBay, the code you've provided shows that its all in Form1 and it should be in the external unit

I've got a Form and then I've got a unit, the Make and Show Procedure are in the Unit, and is Called from within the Form OnCreate.


the Procedures have to be within the external Unit file.

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Ok try it this way:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;
type
  TForm1 = class(TForm)
  MainMenu1: TMainMenu;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses Unit2;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
     MyMenuHandler := TMyMenuHandler.Create(MainMenu1);
end;
end. // Main Unit ***********

//********* Unit 2 ** Extenal Unit
unit Unit2;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;


Type TMyMenuHandler = class(TObject)
  constructor Create(MainMenu : TMainMenu);
  Procedure Show(Sender : TObject);
end;

var MyMenuHandler : TMyMenuHandler;

implementation

constructor TMyMenuHandler.Create(MainMenu: TMainMenu);
var Item, SubItem: TMenuItem;
begin
  Item := TMenuItem.Create(MainMenu);
  Item.Caption := '&File';
  MainMenu.Items.Add(item);

  SubItem := TMenuItem.Create(MainMenu);
  SubItem.Caption := '&Open';
  SubItem.OnClick := Show;
  SubItem.Name := 'mnuOpen';
  subItem.Tag := 1;
  Item.Add(SubItem);

  SubItem := TMenuItem.Create(MainMenu);
  SubItem.Caption := 'E&xit';
  SubItem.OnClick := Show;
  SubItem.Name := 'mnuExit';
  subItem.Tag := 2;
  Item.Add(SubItem);

end;

procedure TMyMenuHandler.Show(Sender: TObject);
begin
     if Sender is TMenuItem then
          begin
          case TMenuItem(Sender).Tag of
          1 : begin
               // Put Open code here
              ShowMessage('Open Clicked');
              end;
          2 : begin
               // Put Exit Code Here
              ShowMessage('Exit Clicked');
              end;
          end; //case
          end;
end;


end.

Open in new window

In my haste I forgot to include this
procedure TForm1.FormDestroy(Sender: TObject);
begin
     MyMenuHandler.Free;
end;

Open in new window

Author

Commented:
waw! neat! it works! so you can actually have a constructor within the unit, wasn't aware of that, so the constructor, now acts like its own addon component?

is there a reason behind the MyMenuHandler.Free on the FormDestroy? is it needed as the app is already closing

Author

Commented:
Thanks!
A unit is just the collection  of code into a file. It's the TObject that has a constructor. Any TObject can have a Constructor and while a Component is a decendant of a TObect (TComponent to be specific) TObject is not a TComponent.

MyMenuHandler.Free - Because neatness counts!


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial