Page 1 of 1
Forum

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.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - TTreeview - Prevent popup menu when not clicking a node

1 Posts
1 Users
0 Reactions
2,501 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

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);
begin
  if Treeview.GetNodeAt(MousePos.X, MousePos.Y) = nil then
    Handled := True;
end;   

   
ReplyQuote
Share: