• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1867
  • Last Modified:

Create mainmenu at runtime

Hi,
I have created a program that handles different files.
Now I want to create a "historymenu" for the most rescent used files.
My problem is how to create new menuitems in my excisting mainmenu.
Anyone any idea?

// svenj
0
svenj
Asked:
svenj
1 Solution
 
sburckCommented:
The Delphi help includes this:  umnudmo.pas

This shows how to programatically build up a TMainMenu, adding new TMenuItems.  The main thrust is, do TMenuItems.Create, then add them to the mainmenu.

0
 
freterCommented:
from my PAQ (http://www1.experts-exchange.com/bin/Q.10132229)

basically the same...

Q:
How can I create TMenuItems when the application is running?
How can I assign methods to the items created?

A:
(From: freter
 Date: Wednesday, February 24 1999 - 04:13AM PST )

Here we go:
>>>>>>>>>>>>>>>>>>>><< 
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ExitClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
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 := ExitClick;
  item.Add(item2);

end;

procedure TForm1.ExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

end.
<<<<<<<<<<<<<<<<<<<< 

Don't forget to place a TMainMenu named MainMenu1 and a TButton named Button1 onto your form. Also, please take care to attach the OnClick event to the Button1Click method.

Have fun,
<freter>
0
 
shenqwCommented:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    MainMenu:TMainMenu;
    procedure MenuClick(Sender:TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  MenuItem:TMenuItem;
  SubMenuItem:TMenuItem;
begin
  if MainMenu<>nil then Exit;
  MainMenu:=TMainMenu.Create(Self);
  MenuItem:=TMenuItem.Create(Self);
  MenuItem.Caption:='&File';
  SubMenuItem:=TMenuItem.Create(Self);
  SubMenuItem.Caption:='&SubFile';
  SubMenuItem.OnClick:=MenuClick;
  MenuItem.Add(SubMenuItem);
  MainMenu.Items.Add(MenuItem);
  Self.Menu:=MainMenu;
end;

procedure TForm1.MenuClick(Sender: TObject);
begin
  ShowMessage(TMenuItem(Sender).Caption);
end;

end.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now