eNarc
asked on
MainMenu
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.Mai nMenu1);
item.Caption := '&File';
Form7.MainMenu1.Items.Add( item);
item2 := tmenuitem.create(Form7.Mai nMenu1);
item2.Caption := 'E&xit';
item2.OnClick := Show;
item.Add(item2);
end;
end.
########################## ########## ########## #######
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.Mai
item.Caption := '&File';
Form7.MainMenu1.Items.Add(
item2 := tmenuitem.create(Form7.Mai
item2.Caption := 'E&xit';
item2.OnClick := Show;
item.Add(item2);
end;
end.
##########################
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.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In my haste I forgot to include this
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyMenuHandler.Free;
end;
ASKER
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
is there a reason behind the MyMenuHandler.Free on the FormDestroy? is it needed as the app is already closing
ASKER
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!
MyMenuHandler.Free - Because neatness counts!
Open in new window