Mega Code Archive

 
Categories / Delphi / Hardware
 

Right mouse button fix in TTreeView and TShellTreeView

Title: Right mouse button fix in TTreeView and TShellTreeView Question: In Delphi 7 (possibly in other versions too), when you try to select a node in TTreeView or TShellTreeView controls with the right mouse button, the node is selected, but when you release the mouse button, the selection returns to the previously selected node. Answer: This strange behavior was detected in Delphi 7.0, in TTreeView and TShellTreeView controls. To solve this problem you need to add a few lines of code. In TTreeView control I choose the ContextPopup event. Example: //-------------------------------------------------------------------------- procedure TForm1.TreeViewContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); var node: TTreeNode; begin node := TreeView.GetNodeAt(MousePos.X, MousePos.Y); // Tries to get node if (node nil) then TreeView.Selected := node; // Pass selection to tree end; //-------------------------------------------------------------------------- A new problem appears here. The TShellTreeView control doesn't have the ContextPopup event, so you can add this fix to the MouseDown event. Example: //-------------------------------------------------------------------------- procedure TForm1.ShellTreeViewMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var node: TTreeNode; begin if (Button = mbRight) then begin // Right mouse button node := TreeView.GetNodeAt(X, Y); // Tries to get node if (node nil) then TreeView.Selected := node; // Pass selection to tree end; end; //-------------------------------------------------------------------------- Thats all for now. G00d luck.- q:B