如何更改表单菜单

时间:2014-05-27 02:56:34

标签: delphi menu delphi-xe4

我有一个最初使用Delphi 2005编写的应用程序。

它有一个页面控件,根据选择的选项卡,它会显示一个不同的菜单。

case PageControl.ActivePageIndex of
  0: begin
    Menu := HeaderMenu;
  end;
  1: begin
    Menu := DetailMenu;
  end;
end;

在XE中,这不起作用,菜单不会改变。如果选择新选项卡,如何将表单菜单替换为另一个?

以下显示我想要做的事情。不幸的是它按预期工作。 M app有100多个表单,所以我无法发布所有

主要表格

unit Unit5;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus;

type
  TForm5 = class(TForm)
    MainMenu1: TMainMenu;
    MainMenu2: TMainMenu;
    OpenSecondWindow1: TMenuItem;
    Dummy1: TMenuItem;
    SwitchMenus1: TMenuItem;
    Dummy2: TMenuItem;
    SwitchMenus2: TMenuItem;
    OpenSecondWindow2: TMenuItem;
    procedure SwitchMenus2Click(Sender: TObject);
    procedure OpenSecondWindow1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

uses Unit6;

procedure TForm5.OpenSecondWindow1Click(Sender: TObject);
begin
  Form6.Show();
end;

procedure TForm5.SwitchMenus2Click(Sender: TObject);
begin
  if Menu = MainMenu1 then
    Menu := MainMenu2
  else
    Menu := MainMenu1;
end;

end.

第二表格

unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ComCtrls, Vcl.ExtCtrls;

type
  TForm6 = class(TForm)
    MainMenu1: TMainMenu;
    MainMenu2: TMainMenu;
    PageControl1: TPageControl;
    Menu11: TMenuItem;
    File1: TMenuItem;
    insert1: TMenuItem;
    save1: TMenuItem;
    Menu21: TMenuItem;
    Stuff1: TMenuItem;
    Save2: TMenuItem;
    Donoting1: TMenuItem;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    Panel1: TPanel;
    procedure PageControl1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

procedure TForm6.PageControl1Change(Sender: TObject);
begin
  case PageControl1.TabIndex of
    0: Menu := MainMenu1;
    1: Menu := MainMenu2;
    2: Menu := nil;
  end;
end;

end.

1 个答案:

答案 0 :(得分:2)

经过测试并且有效:

procedure TfmMain.PageControlChange(Sender: TObject);
begin
  case PageControl.ActivePageIndex of
    0: Menu := MainMenu1;
    else Menu := MainMenu2;
  end;
end;

注意:在表单上放置第一个MainMenu时,表单属性“Menu”设置为MainMenu1。
在XE3中测试