C # double-click event of the Treeview Node

Source: Internet
Author: User
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

}

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.