赞
踩
public virtual ToolStripItem this[int index] { get; }
public virtual ToolStripItem this[string key] { get; }
private ToolStrip toolStrip1; //菜单栏
private ToolStripContainer toolStripContainer1; //面板,放于顶部存放toolStrip1
private ToolStripDropDown dropDown; //按扭ToolStripDropDownButton弹出来的子菜单容器,二级菜单
private ToolStripDropDown dropDown2; //按扭ToolStripDropDownButton弹出来的子菜单容器,三级菜单
private void Form1_Load(object sender, EventArgs e)
{
toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
toolStrip1 = new System.Windows.Forms.ToolStrip();
dropDown = new ToolStripDropDown();
dropDown2 = new ToolStripDropDown();
//一级菜单
//创建各种类型的子项
ToolStripButton toolStripButton = new ToolStripButton();
toolStripButton.Text = "1-button";
ToolStripLabel toolStripLabel = new ToolStripLabel();
toolStripLabel.Text = "1-text";
ToolStripDropDownButton toolStripDropDownButton = new ToolStripDropDownButton();
toolStripDropDownButton.Text = "1-DownButton";
ToolStripSplitButton toolStripSplitButton = new ToolStripSplitButton();
toolStripSplitButton.Text = "1-splitButton";
ToolStripTextBox toolStripTextBox = new ToolStripTextBox();
toolStripTextBox.Text = "1-textBox";
ToolStripComboBox toolStripComboBox = new ToolStripComboBox();
toolStripComboBox.Text = "1-ComboBox";
//这里使用了参数为string类型的重载
toolStrip1.Items.Add("1-One");
//下面使用的是参数为ToolStripItrm的重载
toolStrip1.Items.Add(toolStripButton);
toolStrip1.Items.Add(toolStripLabel);
toolStrip1.Items.Add(toolStripDropDownButton); //将弹出二级菜单
toolStrip1.Items.Add(toolStripSplitButton);
toolStrip1.Items.Add(toolStripTextBox);
toolStrip1.Items.Add(toolStripComboBox);
// 将ToolStrip添加到ToolStripContainer的顶部面板
toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1);
toolStrip1.Dock = DockStyle.Fill;
// 把顶部面板添加到窗体的控件集合里
Controls.Add(toolStripContainer1);
toolStripContainer1.Width = this.Width;
//二级菜单
//创建二级各种类型的子项
ToolStripButton toolStripButton2 = new ToolStripButton();
toolStripButton2.Text = "2-button";
ToolStripLabel toolStripLabel2 = new ToolStripLabel();
toolStripLabel2.Text = "2-text";
ToolStripDropDownButton toolStripDropDownButton2 = new ToolStripDropDownButton();
toolStripDropDownButton2.Text = "2-DownButton";
ToolStripSplitButton toolStripSplitButton2 = new ToolStripSplitButton();
toolStripSplitButton2.Text = "2-splitButton";
ToolStripTextBox toolStripTextBox2 = new ToolStripTextBox();
toolStripTextBox2.Text = "2-textBox";
ToolStripComboBox toolStripComboBox2 = new ToolStripComboBox();
toolStripComboBox2.Text = "2-ComboBox";
//添加二级菜单项到二级菜单容器
dropDown.Items.Add(toolStripButton2);
dropDown.Items.Add(toolStripLabel2);
dropDown.Items.Add(toolStripDropDownButton2);
dropDown.Items.Add(toolStripSplitButton2);
dropDown.Items.Add(toolStripTextBox2);
dropDown.Items.Add(toolStripComboBox2);
//把二级菜单容器绑定到一级菜单某个项
toolStripDropDownButton.DropDown = dropDown;
//三级菜单
//创建三级各种类型的子项
ToolStripButton toolStripButton3 = new ToolStripButton();
toolStripButton3.Text = "3-button";
ToolStripLabel toolStripLabel3 = new ToolStripLabel();
toolStripLabel3.Text = "3-text";
ToolStripDropDownButton toolStripDropDownButton3 = new ToolStripDropDownButton();
toolStripDropDownButton3.Text = "3-DownButton";
ToolStripSplitButton toolStripSplitButton3 = new ToolStripSplitButton();
toolStripSplitButton3.Text = "3-splitButton";
ToolStripTextBox toolStripTextBox3 = new ToolStripTextBox();
toolStripTextBox3.Text = "3-textBox";
ToolStripComboBox toolStripComboBox3 = new ToolStripComboBox();
toolStripComboBox3.Text = "3-ComboBox";
//添加三级菜单项到三级菜单容器
dropDown2.Items.Add(toolStripButton3);
dropDown2.Items.Add(toolStripLabel3);
dropDown2.Items.Add(toolStripDropDownButton3);
dropDown2.Items.Add(toolStripSplitButton3);
dropDown2.Items.Add(toolStripTextBox3);
dropDown2.Items.Add(toolStripComboBox3);
//把三级菜单容器绑定到二级菜单某个项
toolStripDropDownButton2.DropDown = dropDown2;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
//遍历一级菜单
foreach (ToolStripItem item in toolStrip1.Items)
{
listBox1.Items.Add(item.Text);
//判断该项是否为二级菜单
if (item is ToolStripDropDownItem)
{
listBox1.Items.Add("");
listBox1.Items.Add("以下是" + item.Text + "的子项☞");
ToolStripDropDownItem toolStripDropDownItem = item as ToolStripDropDownItem;
TracerseToolStrip(toolStripDropDownItem);
listBox1.Items.Add("以上是" + item.Text + "的子项☜");
listBox1.Items.Add("");
}
}
}
/// <summary>
/// 递归遍历二级以上菜单
/// </summary>
/// <param name="toolStripItem"></param>
private void TracerseToolStrip(ToolStripDropDownItem toolStripItem)
{
foreach (ToolStripItem item in toolStripItem.DropDownItems)
{
listBox1.Items.Add(item.Text);
//判断该项是否为二级菜单
if (item is ToolStripDropDownItem)
{
listBox1.Items.Add("");
listBox1.Items.Add("以下是" + item.Text + "的子项☞");
ToolStripDropDownItem toolStripDropDownItem = item as ToolStripDropDownItem;
TracerseToolStrip(toolStripDropDownItem);
listBox1.Items.Add("以上是" + item.Text + "的子项☜");
listBox1.Items.Add("");
}
}
}
}
不管多少及,都能遍历成功
这里分两次遍历,主要考虑到一级菜单的特殊性。当然ToolStrip本身就是ToolStripDropDown的父类,用一个递归也能全部遍历完。有兴趣的同学可以自己尝试一下。
注:
转载本文需要标明出处!
谷子彭:1062484747@163.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。