C # traverse all menus in the contextmenustrip context menu that contain sub-menus and add events

Source: Internet
Author: User

Write todayProgramRight-click the menu control contextmenustrip, which is really convenient. I wrote a few menus and sub-menu items, which can be used to add events to each menu by double-clicking. However, this seems to be very troublesome, and it is not easy to control some sub-menus, so I want to use the contextmenustrip Traversal method to add events to each sub-menu separately, but Baidu has a bit, I found that there was very little information in this area. After studying it, I successfully compiled this function.CodeAlthough it is very simple, it also hopes that fewer people will follow unnecessary detours. The reason for this is that there is actually nothing technically, it is just a foreach, there are only a few objects. Start work. 1, first or the old one, create a Windows form application project, the name is not important, here I named http://www.xiaoyaolife.com/2, add two picturebox controls in the form, one is picturebox1, one is picturebox2, showing two different pictures respectively. Drag a contextmenustrip control from the toolbar, name it xiaoyaolife_menu, add several menus, select two pictureboxes respectively, find the attribute contextmenustrip, and select the right-click menu control xiaoyaolife_menu we just added from the drop-down list.

3. the following code shows how to add custom events to each menu item right-click.

 
Void addmenuevents (contextmenustrip m) {// traverse all items in the menu
 
// You can add a menu here, for example:
// M. Items. Add ("delete ");
 
// M. Items. Add ("Update ");
 
 
 
Foreach (toolstripitem items in M. Items)
 
{
 
// Click a menu to add a sub-menu. The Code is as follows:
 
// If (items. Text = "delete ")
 
//{
 
// Toolstripitem ts_1 = new toolstripmenuitem ("normal ");
// (Toolstripdropdownitem) (items). dropdownitems. Add (ts_1 );
 
//}
 
// If it is a menu item, the result is that you can add other items in the context menu. If (items is toolstripmenuitem) {childmenu (toolstripmenuitem) items );}}} void childmenu (toolstripmenuitem menu) {If (menu. hasdropdownitems) {// If a sub-menu foreach (toolstripmenuitem m in menu. dropdownitems) {childmenu (m) ;}} else {// Add the Custom Event xiaoyaolife_event menu directly if no sub-menu exists. click + = new eventhandler (xiaoyaolife_event );}}

4. The code for the Custom Event xiaoyaolife_event is as follows:

 

 
Void xiaoyaolife_event (Object sender, eventargs e) {toolstripmenuitem Ts = (sender as toolstripmenuitem );
 
// Obtain the upper menu string oweraitem = ts. owneritem. Text of the sub-menu;
 
String thecmdstr = ts. text; Switch (thecmdstr) {Case "Who am I": MessageBox. show (thecmdstr); break; Case "website domain name": MessageBox. show ("http://www.xiaoyaolife.com/"); break; Case "website name": MessageBox. show ("happy life"); break; default: break ;}}

5. Add the following in form_load:

Private void form1_load (Object sender, eventargs e) {addmenuevents (xiaoyaolife_menu );}

6. OK. You can debug it.
Click "show text"-"website domain name:

For other related content, click:
1. To know which control the user has clicked, click the following link.
C # determine the control from which the right-click menu (contextmenustrip) pops up
:

 

2. How can a similar menu in the contextmenustrip submenu make a menu selected.
:

 

 

Add a sub-menu to a fixed menu

Toolstripitem ts_1 = new toolstripmenuitem ("normal ");

Ts_1.click + = new eventhandler (ts_1_click );

You can only use the index of the item in contextmenustrip to add a sub-menu to it. An error will be reported through the text attribute of the item */

(Toolstripdropdownitem) (contextmenustrip1.items [1]). dropdownitems. Add (ts_1 );

Toolstripitem ts_2 = new toolstripmenuitem ("Repair ");

Ts_2.click + = new eventhandler (ts_2_click );

(Toolstripdropdownitem) (contextmenustrip1.items [1]). dropdownitems. Add (ts_2 );

Toolstripitem ts_3 = new toolstripmenuitem ("under construction ");

Ts_3.click + = new eventhandler (ts_3_click );

(Toolstripdropdownitem) (contextmenustrip1.items [1]). dropdownitems. Add (ts_3 );

Source code download

Related Article

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.