Page 1 of 1

Lazarus – TTreeview – Prevent popup menu when not clicking a node

Lazarus – TTreeview – Prevent popup menu when not clicking a node

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.

RSS Feed

Home Forums Software Development Delphi, Lazarus, Free Pascal Lazarus – TTreeview – Prevent popup menu when not clicking a node

This topic contains 0 replies, has 1 voice, and was last updated by  hans 5 months, 1 week ago.

Viewing 1 post (of 1 total)
  • Author
  • 11975


    While working on ApplePi-Baker v2, I ran into an issue with TTreeView.

    When right clicking the TreeView, I wanted to display a popup menu, based on the selected node (ttreeview.multiselect = false).
    To my surprise, TTreeView doesn’t really seem to care if you’re right clicking a node or not.
    So right clicking in blank space (no node under the mouse cursor) actually does popup the menu, and I couldn’t find an easy and reliable way to determine if the mouse was (still) over a node of the treeview.

    The trick is to prevent the popup menu from appearing when the mouse cursor is not over a node and this can be done with the OnContextPopup event.
    The OnContextPopup event has a “Handled” variable. If set to TRUE, Treeview considers the context action to be done and dealt with.
    This event also passes the mouse cursor position, so we can use GetNodeAt() to determine what node the mouse cursor was clicking.

    All combined in simple code:

    procedure TForm1.TreeviewContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
      if Treeview.GetNodeAt(MousePos.X, MousePos.Y) = nil then
        Handled := True;
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.