实现效果:
知识运用:
DropDownItems菜单项集合的Add方法 //将指定的菜单项添加到菜单项集合
public int Add(ToolStripItem value)
实现代码:
private void Form1_Load(object sender, EventArgs e) { ToolStripMenuItem P_ts = (ToolStripMenuItem)menuStrip1.Items[0]; //得到文件菜单项 ToolStripMenuItem ts1 = new ToolStripMenuItem("打开文本文件"); ToolStripMenuItem ts2 = new ToolStripMenuItem("打开XML文件"); //创建菜单项 ToolStripMenuItem ts3 = new ToolStripMenuItem("打开JPG文件"); ToolStripMenuItem ts4 = new ToolStripMenuItem("打开BMP文件"); ToolStripMenuItem P_ts2 = (ToolStripMenuItem)P_ts.DropDownItems[0]; //得到子菜单项 P_ts2.DropDownItems.Add(ts1); P_ts2.DropDownItems.Add(ts2); //添加菜单项 P_ts2.DropDownItems.Add(ts3); P_ts2.DropDownItems.Add(ts4); }