弹出菜单没有出现在我的Delphi中

时间:2013-07-06 12:25:10

标签: delphi delphi-xe2

表单上有TPopupMenu和三个按钮,名为“AddButton”,“EditButton”和“DestroyButton”,并为所有三个按钮添加了OnClick事件。表单的PopupMenu属性中的TPopupMenu。我在TForm1类型声明中创建了PopupMenuItemsClick过程,以便它可以用作菜单项OnClick事件的方法调用。

type
  TForm1 = class(TForm)
    AddButton: TButton;
    EditButton: TButton;
    DestroyButton: TButton;
    PopupMenu1: TPopupMenu;
    procedure AddButtonClick(Sender: TObject);
    procedure EditButtonClick(Sender: TObject);
    procedure DestroyButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    procedure PopupMenuItemsClick(Sender: TObject);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AddButtonClick(Sender: TObject);
var
  index: Integer;
  NewItem: TMenuItem;
begin
        // The owner (PopupMenu1) will clean up this menu item.
  NewItem := TMenuItem.Create(PopupMenu1); // Create the new item.
  index := PopupMenu1.Items.Count;
  PopupMenu1.Items.Add(NewItem);// Add it to the pop-up menu.
  NewItem.Caption := 'Menu Item ' + IntToStr(index);
  NewItem.Tag := index;
  NewItem.OnClick :=
    PopupMenuItemsClick; // Assign it an event handler.
end;

procedure TForm1.PopupMenuItemsClick(Sender: TObject);
begin
  with Sender as TMenuItem do
  begin
    case Tag of
      0:  ShowMessage('first item clicked');
      1:  ShowMessage('second item clicked');
      2:  ShowMessage('third item clicked');
      3:  ShowMessage('fourth item clicked');
    end;
  end;
end;

{
To edit or destroy an item, grab its pointer 
using the Items property.
procedure TForm1.EditButtonClick(Sender: TObject);
var
  ItemToEdit: TMenuItem;
begin
  ItemToEdit := PopupMenu.Items[1];
  ItemToEdit.Caption := 'Changed Caption';
end;

procedure TForm1.DestroyButtonClick(Sender: TObject);
var
  ItemToDelete: TMenuItem;
begin
  ItemToDelete := PopupMenu.Items[2];
  ItemToDelete.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  index: Integer;
  NewItem: TMenuItem;
begin
  for index := 0 to 3 do
  begin
          // The owner (PopupMenu1) will clean up this menu item.
    NewItem := TMenuItem.Create(PopupMenu1); // Create the new item.
    PopupMenu1.Items.Add(NewItem);// Add it to the pop-up menu.
    NewItem.Caption := 'Menu Item ' + IntToStr(index);
    NewItem.Tag := index;
    NewItem.OnClick :=
      PopupMenuItemsClick; // Assign it an event handler.
  end;
end;

但是当我点击addmenu按钮时,PopupMenu没有出现。任何人都可以找到在加载表单或点击任何按钮时没有出现Popupmenu的原因。

3 个答案:

答案 0 :(得分:3)

你的代码不是你真正需要的 使用此代码,它将完美地工作

procedure TForm1.PopupMenuItemsClick(Sender: TObject);
  var ICount : Integer;
begin
  ICount := TMenuItem(Sender).MenuIndex;
  ShowMessage('Item Number '+ IntToStr(ICount+1) + ' Selected');
end;

procedure TForm1.AddClick(Sender: TObject);
  var
    Index: Integer;
    NewItem: TMenuItem;
begin
    NewItem := TMenuItem.Create(PopupMenu);
    Index := PopupMenu.Items.Count;
    PopupMenu.Items.Add(NewItem);
    NewItem.Caption := 'Menu Item ' + IntToStr(Index);
    NewItem.Tag := Index;
    NewItem.OnClick := PopupMenuItemsClick;
    PopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

我用Delphi7,XE2和XE3测试了它的工作

答案 1 :(得分:1)

在FormCreate中添加此行或在Object Inspector中设置此属性。     self.PopupMenu:=PopupMenu1;

答案 2 :(得分:0)

这个评论可能有助于解决问题(我在旧的Delphi版本中有类似的,我没有XE可以测试)。

  • 永远不要创建一个组件,并将其.Name清空 给它一个唯一的值(当我们为空时,我看到很多错误的内部代码,因为它们不能为空)。
  • 并且在添加之前总是将属性和事件分配给组件 他们是他们的父母。

在评论中查看此建议:

procedure TForm1.AddClick(Sender: TObject);
  var
    Index: Integer;
    NewItem: TMenuItem;
begin
    NewItem := TMenuItem.Create(PopupMenu);
    Index := PopupMenu.Items.Count;

    //PopupMenu.Items.Add(NewItem); // Not the correct place, see below
    NewItem.Name : = 'SomeText' + IntToStr(Index); // Name them, with a unique name not starting with a number (also there is no need to put a number)

    NewItem.Caption := 'Menu Item ' + IntToStr(Index);
    NewItem.Tag := Index;
    NewItem.OnClick := PopupMenuItemsClick;

    PopupMenu.Items.Add(NewItem); // After properties has been set, never before

    PopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
    // Do not forget to free such menu item somewhere on your code, obviously not here
end;

使用菜单时,请记住释放创建的项目,他们不会自由释放,下次将使用名称。