日期:2014-05-17 浏览次数:21285 次
public void ShowControl(ToolStripItemCollection items) { foreach (ToolStripMenuItem item in items) { MessageBox.Show(item.Text); if (item.DropDownItems.Count > 0) { ShowControl(item.DropDownItems); } } } private void button1_Click(object sender, EventArgs e) { ShowControl(this.menuStrip1.Items); }
------解决方案--------------------
新建一个Windows Forms应用,添加一个按钮到窗体上,在单击事件里面写:
private void button1_Click(object sender, EventArgs e) { MenuStrip ms = new MenuStrip(); ToolStripMenuItem tsmi = new ToolStripMenuItem("视图1"); ToolStripMenuItem tsmi1 = new ToolStripMenuItem("视图1-1"); ToolStripMenuItem tsmi11 = new ToolStripMenuItem("视图1-1-1"); ToolStripMenuItem tsmi12 = new ToolStripMenuItem("视图1-1-2"); tsmi1.DropDownItems.AddRange(new ToolStripItem[] { tsmi11, tsmi12 }); ToolStripMenuItem tsmi2 = new ToolStripMenuItem("视图1-2"); ToolStripMenuItem tsmi21 = new ToolStripMenuItem("视图1-2-1"); ToolStripMenuItem tsmi22 = new ToolStripMenuItem("视图1-2-2"); tsmi2.DropDownItems.AddRange(new ToolStripItem[] { tsmi21, tsmi22 }); tsmi.DropDownItems.AddRange(new ToolStripItem[] { tsmi1, tsmi2 }); ms.Items.Add(tsmi); ms.Dock = DockStyle.Top; Controls.Add(ms); MainMenuStrip = ms; }
------解决方案--------------------
- -!、、虽然我没有搞过菜单
但我真心觉得 在窗体设计的时候知道怎么搞 换成代码就不知道怎么搞 就是一个悲剧
如果 你不知道怎么搞 你可以去看看 你在设计窗体的时候 为窗体设计自动生成的代码看看也许 你就能知道点啥了