日期:2014-05-17  浏览次数:20873 次

请问,如何访问菜单控件的子项的子项
如何访问菜单控件(MenuStrip)的子项的子项的子项.....
每个项的类型是ToolStripMenuItem类,子项又是什么类型的,也就是1级项。2级项又是什么类型,3级呢?
如何把某个2级子项设置为隐藏,比如对于非高级会员就可以隐藏某个2级子项。

------解决方案--------------------
顶个 这个应该能实现你想实现的功能就是有些功能有些用户是不可见的,应该能实现。
------解决方案--------------------
C# code
        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);
        }

------解决方案--------------------
新建一个窗体,添加2个按钮,button1 button2,编写如下代码:
C# code
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;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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");
            ToolStripMenuItem tsmi13 = new ToolStripMenuItem("视图1-1-3");
            tsmi1.DropDownItems.AddRange(new ToolStripItem[] { tsmi11, tsmi12, tsmi13 });
            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 });
            ToolStripMenuItem tsmi3 = new ToolStripMenuItem("视图1-3");
            ToolStripMenuItem tsmi31 = new ToolStripMenuItem("视图1-3-1");
            tsmi3.DropDownItems.AddRange(new ToolStripItem[] { tsmi31 });
            tsmi.DropDownItems.AddRange(new ToolStripItem[] { tsmi1, tsmi2, tsmi3 });
            ms.Items.Add(tsmi);
            ms.Dock = DockStyle.Top;
            Controls.Add(ms);
            MainMenuStrip = ms;
        }

        private IEnumerable<ToolStripMenuItem> GetAllMenuItems(ToolStripMenuItem top)
        {
            return top.DropDownItems.Cast<ToolStripMenuItem>().SelectMany(x => GetAllMenuItems(x)).Concat(top.DropDownItems.Cast<ToolStripMenuItem>());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem item in (Controls.OfType<MenuStrip>().FirstOrDefault() ?? new MenuStrip()).Items)
            {
                foreach (ToolStripMenuItem subitem in GetAllMenuItems(item).Where(x => x.Text.Contains("3")))
                {
                    subitem.Enabled = false;
                }
            }
        }
    }
}

------解决方案--------------------
本程序支持无线递归搜索,你可以尝试再添加更深层次的菜单。

在真实使用中,你可以精确搜索你要的菜单项目,代码如下:
var menu = GetAllMenuItems(Controls.OfType<MenuStrip>().FirstOrDefault().Items.Cast<ToolStripMenuItem>().Where(x => x.Text == "主菜单文本").FirstOrDefault()).Where(x => x.Text == "你要找的菜单的文本").FirstOrDefault();
menu.Visibled = false; //隐藏
menu.Enabled = false; //禁用
menu.DropDownItems.Add(new ToolStripMenuItem("xxx")); //添加子