user983145
user983145

Reputation: 219

Check-ToolButton. I need an Event before showing the menu

I have a couple of buttons on a toolbar with the Style defined as tbsDropDown and a Popup menu assigned.

I need to check if there are any records in the Database before showing the Menu. I have added btnFilter.CheckMenuDropDown; to the main part of the button so I am able to check there, but the "Down arrow" button shows the Popup. I need to intervene at that point with an Event of "BeforePopup"

Any suggestions?

Thanks

Upvotes: 0

Views: 551

Answers (2)

NGLN
NGLN

Reputation: 43669

First, TToolButton.CheckMenuDropDown is a routine which always drops down the menu, but returns False when it is not able to.

I understand that you want to disable the dropdown menu when there are no records in de dataset.

Possible solutions:

  • Disable the tool button (or temporarily set the PopupMenu property to nil) when there are no records, and vise versa. This is preferably to be done by attaching an action to the button.
  • Interpose the TToolButton class and override CheckMenuDown:

    type
      TToolButton = class(ComCtrls.TToolButton)
      private
        FCanDropDown: Boolean;
      public
        function CheckMenuDropdown: Boolean; override;
        property CanDropDown: Boolean read FCanDropDown write FCanDropDown;
      end;
    
    function TToolButton.CheckMenuDropdown: Boolean;
    begin
      Result := FCanDropDown and (inherited CheckMenuDropdown);
    end;
    

    But this is not a designtime solution and you have to set the CanDropDown property at runtime accordingly.

  • Create a new component and publish the CanDropDown property in the above code.

Upvotes: 2

Rob Kennedy
Rob Kennedy

Reputation: 163347

Before the menu appears, the its OnPopup event fires. That gives you an opportunity to modify the menu's contents before you finally show it.

Upvotes: 1

Related Questions