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 ();
}