Solved

Create mainmenu at runtime

Posted on 2000-05-11
3
1,674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

738 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