Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rightclick treeview node 1

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I am trying to change popup window contents in a treeview depending on the node that is rightclicked.

It seems that when you release the mouse button after right clicking, the previously selected node gets selected. You first have to left click on the node to keep the node selected. This seems weird. Is there any solution to this so the node remains selected after right clicking?

Thanks,
Raoul
 
The way to keep the TreeView node selected after a right click is as follows:

In the Object Inspector (or at run time) set the RightClickSelect property of the TreeView to TRUE.

Write an OnMouseUp event handler for the TreeView component along the lines of:
Code:
procedure TForm1.TreeView1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   TreeView1.Select ( TreeView1.Selected );
end;

Andrew
Hampshire, UK
 
Still got a problem.

This is the code when selecting 'delete' from the popup menu:

Code:
procedure TTrnmntForm.Delete1Click(Sender: TObject);
  var tmp : string;
begin
  TreeView.Select ( TreeView.Selected );
  DeleteNode(PMyRec(TreeView.Selected.Data)^.NodeID)// database
  TreeView.Selected.Delete;

The node that is deleted is not the one rightclicked on but the previously selected node.

The only workaround for this regarding the database update is setting the NodeID in the TreeView1MouseUp event as e.a. TmpID and use
DeleteNode(TmpID)instead of the line above. But then the wrong node is still deleted.

Is there is a solution for this or should the treeview be rebuilt rebuilt completely after deleting a node?

Thanks,
Raoul
 
I am not sure that this is the best way to do this but it seems to work for me.

Define a couple of variables to save the current mouse X, Y position in Form's class definition.
Code:
  private
    mouseX: integer;
    mouseY: integer;
Write an OnMouseMove event handler to save the mouse position whenever the mouse is moved:
Code:
procedure TForm1.TreeView1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Sender is TTreeView then begin
    mouseX := X;
    mouseY := Y;
  end;
end;
In your OnMouseUp event handler activate your PopUp:
Code:
procedure TForm1.TVMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  PopUpMenu1.Popup(X,Y);
end;
And in your PopUpMenu handler for your Delete processing do what ever you want to do:
Code:
procedure TForm1.Delete1Click(Sender: TObject);
var
  node: TTreeNode;
begin
   node := TV.GetNodeAt(mouseX,mouseY);
// Code to delete database record
end;

Andrew
Hampshire, UK
 
Yes, that solved it untill I rightclicked on another node while the popup was already opened. It seems the OnMouseMove event of the listview is not triggered with an open popup. The wrong node is selected in this case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top