C # Summary of use of menustrip dynamic menu in winform

Source: Internet
Author: User

For reprint, please declare the source:Http://www.freemansoft.net/blog/Article/438.aspx

 

Using menu controls in C # winform, I think menustrip is the first choice for many people. I will not talk much about how to use it. I will talk about my experiences in using it.
Previously, when using this control, you only knew to manually enter the menu item in the Form Designer, which is convenient but not flexible.
A secondary project involves permission assignment. Different users with different permissions need to use different menus. This requires dynamic creation of menustrip and its menu items during form loading.
Later, I searched the internet for a lot of related functions to create dynamic menus.CodeBut it is not ideal. In the end, you can only take the directors and modify and complete the task by yourself.
My idea is as follows:

1. Create a menu table tb_menu in the database., Fields mainly include
--------------------------
ID-Unique id value
Father_id-parent ID of the menu item. If it is a top-level menu, it is 0.
Menu_name-Control name of the menu item in the form
Menu_text-display name of this menu item
Module_action-name of the event to be entertained in this menu item, with parentheses, for example, btncreateuser_click ()
--------------------------
These fields are required, and others are determined based on your actual situation.

2. Drag and Drop a menustrip control in the form named mainmenu1

3. Add the following statement to form_load of the form code: createmenu ();
Of course, in special cases, you can also add the code elsewhere, depending on the actual situation.

4. Add the following constructor to the relevant form code:

Public void createmenu ()
{

// Obtain the statements for all menu items. If you have permission restrictions, modify the statements on your own.
String menusql = "select * From tb_menu ";
// Obtain the menu item Dataset
Dataset DS = dbclass. getdataset (menusql );
// Check whether dataset data is complete
If (checkdata (DS ))
{
// Load the menustrip menu
Toolstripmenuitem topmenu = new toolstripmenuitem ();
Loadsubmenu (ref topmenu, "0 ");
}

}

/// <Summary>
/// Recursively create the menustrip menu (module list)
/// </Summary>
/// <Param name = "topmenu"> parent menu item </param>
/// <Param name = "father_id"> parent menu id </param>
Private void loadsubmenu (ref toolstripmenuitem topmenu, string infatherid)
{
Dataview dvlist = new dataview (menutable );
// Filter out all sub-menu data under the current parent menu (only for the next layer)
Dvlist. rowfilter = "father_id = '" + infatherid + "'";
Toolstripmenuitem submenu;
Foreach (datarowview DV in dvlist)
{
// Create a sub-menu item
Submenu = new toolstripmenuitem ();
Submenu. Name = DV ["menu_name"]. tostring ();
Submenu. Text = DV ["menu_text"]. tostring ();

// Determine whether the menu is a top-level menu
If (infatherid = "0 ")
{
Mainmenu1.items. Add (submenu );
}
Else
{
Submenu. Tag = DV ["module_action"]. tostring ();
String STR = "Void" + DV ["module_action"]. tostring ();
// Add an event to the menu item.
Submenu. Click + = new eventhandler (submenu_click );

Topmenu. dropdownitems. Add (submenu );
}

// Recursive call
Loadsubmenu (ref submenu, DV ["ID"]. tostring ());

}

}

/**/
/// <Summary>
/// Click an event in the menu
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Void submenu_click (Object sender, eventargs E)
{

Try
{
// Tag attributes are useful here.
String acname = (toolstripmenuitem) sender). Tag. tostring ();

If (acname! = "")
{
String [] strarray = acname. Split (New char [] {','});
If (strarray. length> 2)
{
}
Else
{
String STR = "Void" + acname;
Foreach (methodinfo info in base. GetType (). getmethods ())
{
If (Str. Trim (). tolower (). compareto (info. tostring (). Trim (). tolower () = 0)
{
Info. Invoke (this, null );
}
}
}
}
}
Catch (exception)
{

}
}

// Check whether dataset data is complete
Public static bool checkdata (Dataset indata)
{
Bool flag = false;
If (checktable (indata ))
{
For (INT I = 0; I <indata. Tables. Count; I ++)
{
If (indata. Tables. Rows. Count> 0)
{
Flag = true;
}
}
Return flag;
}
Return false;
}
Public static bool checktable (Dataset indata)
{
If (indata = NULL)
{
Return false;
}
Return (indata. Tables. Count> 0 );
}

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.