【问题标题】:Custom Button , OnClick with PopupMenu自定义按钮,带有 PopupMenu 的 OnClick
【发布时间】:2019-11-14 20:40:26
【问题描述】:

所以我创建了一个基于 cxButton 的自定义按钮。我希望在单击此按钮时显示一个弹出菜单。但由于某种原因,弹出菜单没有出现。 我什至没有收到错误,我不知道为什么。

type
  TcxGridButton = class(TcxButton)
  private
    FGridView : TcxGridDBTableView;
    FPopup : TPopupMenu;

    procedure AutoSize(Sender : TObject);
    procedure ClearFilter(Sender : TObject);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;

    procedure Click; override;
  published
    property GridView : TcxGridDBTableView read FGridView write FGridView;
  end;

这是我创建弹出菜单的部分

constructor TcxGridButton.Create(AOwner: TComponent);
var Item : TMenuItem;
    P : TPoint;
begin
  inherited;

  Text:='Options';

  FPopup := TPopupMenu.Create(Self);

  Item := TMenuItem.Create(FPopup);
  Item.Caption:='Nach Excel exportieren';

  Item := TMenuItem.Create(FPopup);
  Item.Caption:='Automatische Größenanpassung';
  Item.OnClick:=AutoSize;

  Item := TMenuItem.Create(FPopup);
  Item.Caption:='Filter löschen';
  Item.OnClick:=ClearFilter;
end;

现在,当我将此按钮放在表单上时,它会立即显示文本选项,因此构造函数似乎运行正常。

但是当我点击这个按钮时,我得到了 Click 、 Self.ToString 和 Done。 但是 Popup 菜单永远不会 Pops up 。我的错误是什么?

procedure TcxGridButton.Click;

begin
  inherited; // call the inherited Click method.

  ShowMessage('CLICK');

  if not Assigned(FGridView) then Exit;

  ShowMessage(Self.ToString);


  FPopup.Popup(0,0);


  ShowMessage('DONE');

end;

【问题讨论】:

    标签: delphi events components


    【解决方案1】:

    答案很简单——您忘记将项目添加到弹出菜单中:

    { after creating each item }
    FPopup.Items.Add(Item);
    

    如果您没有绑定到TCxButton,您可以使用标准 VCL 按钮,该按钮通过属性Style 设置为bsSplitButton 和属性DropDownMenu 来提供您尝试实现的功能。否则,您至少可以研究 VCL 的 TCustomButton 源代码作为您自己实现的灵感。

    【讨论】:

    • 谢谢你,难以置信,我监督了这件事:) 谢谢 - 谢谢 - 谢谢!
    • 我使用了 cxButton ,因为我将这个组件与 cxGrid 挂钩,并且在那里我做了一些我经常做的事情。我刚刚意识到我可以为此创建一个组件,而不是每次都为三个按钮编写代码
    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多