Method pointer and regular procedure incompatible

后端 未结 5 846
谎友^
谎友^ 2020-12-14 03:27

I have an app, which has multiple forms. All these forms have a PopupMenu. I build the menu items programatically, all under a common root menu item. I want ALL the menu

5条回答
  •  無奈伤痛
    2020-12-14 03:52

    You can wrap your procedures into a class. This class might look like this in a separate unit:

    unit CommonUnit;
    
    interface
    
    uses
      Dialogs;
    
    type
      TMenuActions = class
      public
        class procedure BrowseCategoriesClick(Sender: TObject);
      end;
    
    implementation
    
    { TMenuActions }
    
    class procedure TMenuActions.BrowseCategoriesClick(Sender: TObject);
    begin
      ShowMessage('BrowseCategoriesClick');
    end;
    
    end.
    

    And to assign the action to a menu item in a different unit is enough to use this:

    uses
      CommonUnit;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      PopupMenuItem1.OnClick := TMenuActions.BrowseCategoriesClick;
    end;
    

    Update:

    Updated to use class procedures (instead of object methods) by David's suggestion. For those who want to use the object methods with the need of object instance, follow this version of the post.

提交回复
热议问题