The dynamic creation of menus makes the program more flexible, and this article dynamically creates menus based on reading the configuration menu items in the XML file as follows
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using Winformmenu.helper;
Using System.Xml;
Namespace Winformmenu
{
public partial class Frmaddmenu:form
{
Public Frmaddmenu ()
{
InitializeComponent ();
}
private void Frmaddmenu_load (object sender, EventArgs e)
{
MenuStrip MenuS = new MenuStrip ();
Menus.parent = this;
String xmlfilename = @ "f:\c# Project \winformmenu\winformmenu\xml\ menu. XML";
CreateMenu (XMLfileName, MenuS);
}
public void CreateMenu (String xmlfilename, MenuStrip MenuS)
{
1: Create a menu based on XmlElement
XmlNode node = Xmlhelper.getxmlnode (XMLfileName, "//mangetvmenu");
foreach (XmlNode XML in node. ChildNodes)
//{
ToolStripMenuItem mainmenu=new ToolStripMenuItem ();
Mainmenu.text = XML. attributes["Descript"]. Value;
MENUS.ITEMS.ADD (MainMenu);
if (XML. HasChildNodes)
// {
Createsubmenubynode (XML, MainMenu);
// }
//}
2: Create a menu based on XmlNode
XmlElement XML = xmlhelper.getxmlelement (XMLfileName);
foreach (XmlElement xel in XML. ChildNodes)
{
ToolStripMenuItem MainMenu = new ToolStripMenuItem ();
Mainmenu.text = Xel. attributes["Descript"]. Value;
MENUS.ITEMS.ADD (MainMenu);
if (XML. HasChildNodes)
{
Createsubmenubyelement (Xel, MainMenu);
}
}
}
<summary>
Create a menu based on node recursive recursion
</summary>
<param name= "XNode" ></param>
<param name= "MenuItem" ></param>
public void Createsubmenubynode (XmlNode xnode, ToolStripMenuItem MenuItem)
{
foreach (XmlNode xml in Xnode.childnodes)
{
ToolStripMenuItem submenu = new ToolStripMenuItem ();
if (XML. Childnodes.count > 1)
{
Submenu.text = XML. attributes["Descript"]. Value;
MENUITEM.DROPDOWN.ITEMS.ADD (submenu);
Createsubmenubynode (XML, submenu);
}
Else
{
Submenu.text = XML. InnerText;
MENUITEM.DROPDOWN.ITEMS.ADD (submenu);
}
}
}
<summary>
Create a menu recursively based on element node recursion
</summary>
<param name= "xml" ></param>
<param name= "MenuItem" ></param>
public void Createsubmenubyelement (XmlElement xml, ToolStripMenuItem menuItem)
{
foreach (XmlElement xel in XML. ChildNodes)
{
ToolStripMenuItem submenu = new ToolStripMenuItem ();
if (Xel. CHILDNODES.COUNT>1)
{
Submenu.text = Xel. attributes["Descript"]. Value;
MENUITEM.DROPDOWN.ITEMS.ADD (submenu);
Createsubmenubyelement (Xel, submenu);
}
Else
{
Submenu.text = Xel. InnerText;
MENUITEM.DROPDOWN.ITEMS.ADD (submenu);
}
}
}
///////////////////////////////////////////////////
}
}
The XML file format is as follows:
Copy CodeThe code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<mangetvmenu descript= "Management" >
<add descript= "Add" >
<Employee> Staff </Employee>
<Deptment> Department </Deptment>
</Add>
<employeeinfo descript= "Employee Information" >
<BaseInfo> Personal basic Information </BaseInfo>
<StudyInfo> Education Information </StudyInfo>
<MarryInfo> Marriage Information </MarryInfo>
</EmployeeInfo>
<deptmentinfo descript= "Department info" >
<dept1> Department 1</dept1>
<dept2> Department 2</dept2>
</DeptmentInfo>
<moneymanage descript= "Payroll Management" >
<BaseMoney> Basic Salary </BaseMoney>
<OtherMoney> Performance Pay </OtherMoney>
</MoneyManage>
<apply descript= "Application" >
<AddWork> Overtime </AddWork>
<ApplyWeek> Leave </ApplyWeek>
<AddMoney> Pay </AddMoney>
<Another> other </Another>
</Apply>
</MangeTvMenu>
C # WinForm Read the code of the XML file creation menu