C # dynamically add menu and time for permission management

Source: Internet
Author: User
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 for a lot of code related to creating dynamic menus on the Internet, but it was not ideal. Finally, I had to take the various directors and finish the modification myself. My idea is as follows: 1. First, create a menu table tb_menu in the database. The fields mainly include -------------------------- ID-Unique id value father_id-parent ID of the menu item, if it is a top-level menu, it is 0menu_name-Control name of this menu item in the Form menu_text-display name of this menu item module_action-name of the event to be entertained by this menu item, with parentheses, for example: btncreateuser_click () ------------------------ these fields are required. The other fields are determined based on your actual situation. 2. Drag and Drop a menustrip control in the form, named mainmenu13, and add the following statement to form_load of the form code: createmenu (); of course, if the situation is special, you can also add this sentence code elsewhere, depending on the actual situation. 4. Add the following constructor public void createmenu () {// to the relevant form code to obtain all menu items, if you have permission restrictions, modify string menusql = "select * From tb_menu"; // obtain the menu item 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 </PAR Am> /// <Param name = "father_id"> parent menu id </param> private void loadsubmenu (ref toolstripmenuitem topmenu, string infatherid) {dataview dvlist = new dataview (menutable); // filter out the dvlist of all sub-menu data (only for the next layer) under the current parent menu. rowfilter = "father_id = '" + infatherid + "'"; toolstripmenuitem submenu; foreach (datarowview DV in dvlist) {// create submenu item submenu = new toolstripmenuitem (); submenu. name = DV ["menu_name"]. tostring (); s Ubmenu. TEXT = DV ["menu_text"]. tostring (); // determines whether it 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);} // recursively call loadsubmenu (ref submenu, DV ["ID"]. tostring ());}} /** // <summary> // click the event // </Summary> // <Param name = "sender"> </param> // /<Param name = "E"> </param> void submenu_click (Object sender, eventargs e) {try {// tag attribute is 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.