Solved

Create mainmenu at runtime

Posted on 2000-05-11
3
1,647 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
select query - oracle 16 101
Base1 Encode/Decode 3 85
Installshield for Embarcadero EX 10.1 Berlin 4 72
Is there any way to treat Lock record in table with clientdataset? 3 31
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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