This topic describes how to add information in the menustrip control to the Treeview control.
First, add a menustrip control and Treeview control to the winform. Add the following code in the form loading event (depending on your own situation:
/// <Summary>
/// Load the current event in the form
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void frmservermanager_load (Object sender, eventargs E)
{
Treeview1.nodes. Clear (); // clear the navigation menu
// Call the getmenu method to add the child menu of the menustrip1 control to the treeview1 control.
Getmenu (treeview1, menustrip1 );
This. treeview1.expandall ();
}
Then customize the getmenu (treeview1, menustrip1) method. The Code is as follows:
# Region add the information in the menustrip control to the Treeview Control
/// <Summary>
/// Add the information in the menustrip control to the Treeview control.
/// </Summary>
/// <Param name = "treev"> Treeview Control </param>
/// <Param name = "Menus"> menustrip Control </param>
Public void getmenu (Treeview treev, menustrip menus)
{
For (INT I = 0; I <menus. Items. Count; I ++) // traverses the level 1 menu item in the menustrip component
{
// Add the name of the level-1 menu item to the root node of the Treeview component, and set the child node newnode1 of the current node.
Treenode newnode1 = treev. nodes. Add (menus. items [I]. Text );
// Save all relevant information of the current menu item to the toolstripdropdownitem object
Toolstripdropdownitem newmenu = (toolstripdropdownitem) menus. items [I];
// Determine whether there is a level-2 menu item in the current menu item
If (newmenu. hasdropdownitems & newmenu. dropdownitems. Count> 0)
For (Int J = 0; j <newmenu. dropdownitems. Count; j ++) // traverses level-2 menu items
{
// Add the level-2 menu name to newnode1, the child node of the Treeview component, and set newnode2, the child node of the current node.
Treenode newnode2 = newnode1.nodes. Add (newmenu. dropdownitems [J]. Text );
// Save all relevant information of the current menu item to the toolstripdropdownitem object
Toolstripdropdownitem newmenu2 = (toolstripdropdownitem) newmenu. dropdownitems [J];
// Determine whether level-3 menu items exist in level-2 menu items
If (newmenu2.hasdropdownitems & newmenu2.dropdownitems. Count> 0)
For (INT p = 0; P <newmenu2.dropdownitems. Count; P ++) // traverses Level 3 menu items
// Add the level-3 menu name to newnode2, the child node of the Treeview component.
Newnode2.nodes. Add (newmenu2.dropdownitems [p]. Text );
}
}
}
# Endregion
Finally, let's take a look at the following:
Of course, you can also handle the Treeview-related events as needed to display the corresponding application form.