C # Treeview tree control. If no focus is displayed, the right-click menu is displayed when the node is right-clicked. If the blank area is left, no menu is displayed.

Source: Internet
Author: User

Implemented Functions(Tree control-Treeview control of c ):

1. A shortcut menu (right-click menu) is displayed only when you right-click the area where the tree node is located ).

2. When the tree control loses focus, the selected node is still highlighted.

First look at the effect:

Figure 1 highlight when the focus is lost (the color can be set by yourself)

Figure 2 only the menu at the bottom right of the selected node is displayed.

Detailed implementation method (with time to write again ):


Implementation Code:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;

Namespace treeviewdemo
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();

// Clear
Treeview1.nodes. Clear ();
// Treeview1.hideselection = false;
Textbox1.text = "Node ";
}

// When the focus is lost
Private void treeview1_leave (Object sender, eventargs E)
{
If (treeview1.selectednode! = NULL)
{
// Make the selected background color red
Treeview1.selectednode. backcolor = color. Red;
// The foreground color is white.
Treeview1.selectednode. forecolor = color. White;
}
}

// Triggered when you click a node
Private void treeview1_nodemouseclick (Object sender, treenodemouseclickeventargs E)
{
// Right-click
If (E. Button = mousebuttons. Right)
{
// Select the clicked Node
Treeview1.selectednode = E. node;
// Obtain the coordinate value in the lower right corner of the node area
Point Pos = new point (E. node. bounds. x + E. node. bounds. Width, E. node. bounds. Y + E. node. bounds. Height );
// In the lower right corner of the selected node, the right-click menu is displayed.And set the Controller to Treeview.
Contextmenustrip1.show (treeview1, POS );
}
}

// Occurs before the new node is to be selected
Private void treeview%beforeselect (Object sender, treeviewcanceleventargs E)
{
If (treeview1.selectednode! = NULL)
{
// Restore the background color of the last selected node (no color originally)
Treeview1.selectednode. backcolor = color. empty;
// Restore the foreground
Treeview1.selectednode. forecolor = color. Black;
}
}
}
}

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.