Dynamically add information in the menustrip control to the Treeview Control

Source: Internet
Author: User

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.

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.