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;
}
}
}
}