Solved

Create mainmenu at runtime

Posted on 2000-05-11
3
1,534 Views
Last Modified: 2008-03-10
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
Comment
Question by:svenj
3 Comments
 
LVL 3

Accepted Solution

by:
sburck earned 100 total points
ID: 2801007
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
 
LVL 2

Expert Comment

by:freter
ID: 2801554
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
 
LVL 3

Expert Comment

by:shenqw
ID: 2803323
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now