Method 1: (slightly defective)
In the Treeview The selected node is determined in the DoubleClick event of the entire tree. However, this method has a disadvantage. After a node is selected, double-clicking other blank areas triggers the double-click event of the node.
Method 2: (it can better solve such problems) Key steps: 1. Get the current active node based on the mouse position. Of course, this step may still be unexpected, because the blank position on the right side of the treenode is counted as its region.
2. Determine whether the mouse position is on the node. You can introduce a member variable to record the position where the mouse is pressed or popped up. You can also dynamically capture the current absolute position of the mouse, then, use the pointtoclient method of the Treeview control to convert it to the relative position inside the space to check whether it is within the node range. Private point pi;
Private void treeviewinclumousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
Pi = new point (E. X, E. y );
}
Private void treeview1_doubleclick (Object sender, system. eventargs E)
{
Treenode node = This. treeview1.getnodeat (PI );
If (PI. x <node. bounds. Left | pi. x> node. bounds. Right)
{
This. Text = ""; // do not trigger the event
Return;
}
Else
{
This. Text = "ggg"; // trigger the event
}
}