Salvador
Salvador

Reputation: 16472

How i can popup a context menu in a treeview but just in some particular treenodes?

I have a TPopupMenu associated with a TTreeView, but i now want to invoke (popup) the menu only when the user click in a particular node. So how i can popup a context menu in a treeview but just in some particular treenodes?

Upvotes: 4

Views: 3280

Answers (2)

Azad
Azad

Reputation: 5264

This might be helpful:

procedure TForm1.TreeView1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
  var node : TTreeNode;
begin
  node := TreeView1.GetNodeAt(MousePos.X, MousePos.Y);
  if not Assigned (node) then
    Abort;
end;

Upvotes: 1

TLama
TLama

Reputation: 76663

Use the Handled parameter from the OnContextPopup event. By setting this parameter to True you will suppress the context menu to display. The following code shows how to get the TTreeNode from the cursor position passed into the OnContextPopup event and it displays the popup menu only when you right click over the TTreeNode different from the top one.

procedure TForm1.TreeView1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
  if TreeView1.GetNodeAt(MousePos.X, MousePos.Y) = TreeView1.TopItem then
    Handled := True;
end;

Upvotes: 12

Related Questions