C # Winform dynamic menu Addition

Source: Internet
Author: User

I have been developing WINFORM recently, and have been working hard on click events and dynamic menu loading on the main interface. Now this problem has been solved. You can dynamically generate menus and load events by configuring databases or all XML files. The Code is as follows:

Private void Form1_Load (object sender, EventArgs e)
{
// Add menu 1
ToolStripMenuItem subItem;
SubItem = AddContextMenu ("warehouse receiving", menuStrip1.Items, null );
// Add a sub-menu
AddContextMenu ("add warehouse receiving", subItem. DropDownItems, new EventHandler (MenuClicked ));
AddContextMenu ("warehouse receiving management", subItem. DropDownItems, new EventHandler (MenuClicked ));

// Add menu 2
SubItem = AddContextMenu ("warehouse picking", menuStrip1.Items, null );
// Add a sub-menu
AddContextMenu ("add warehouse picking", subItem. DropDownItems, new EventHandler (MenuClicked ));
AddContextMenu ("warehouse picking Management", subItem. DropDownItems, new EventHandler (MenuClicked ));
}

 

/// <Summary>
/// Add a sub-menu
/// </Summary>
/// <Param name = "text"> the text to be displayed. If it is-, it is displayed as a split line. </param>
/// <Param name = "cms"> sub-menu set to be added </param>
/// <Param name = "callback"> events triggered upon clicking </param>
/// <Returns> the generated sub-menu. If it is a separator, null is returned. </returns>

ToolStripMenuItem AddContextMenu (string text, ToolStripItemCollection cms, EventHandler callback)
{
If (text = "-")
{
ToolStripSeparator tsp = new ToolStripSeparator ();
Cms. Add (tsp );
Return null;
}
Else if (! String. IsNullOrEmpty (text ))
{
ToolStripMenuItem tsmi = new ToolStripMenuItem (text );
Tsmi. Tag = text + "TAG ";
If (callback! = Null) tsmi. Click + = callback;
Cms. Add (tsmi );

Return tsmi;
}

Return null;
}

Void MenuClicked (object sender, EventArgs e)
{
// The following describes how to dynamically generate an event and open a form.

// (Sender as ToolStripMenuItem). Tag) forced conversion

ObjectHandle t = Activator. CreateInstance ("WinForms", "WinForms. Form2 ");
Form f = (Form) t. Unwrap ();
F. ShowDialog ();

}

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.