在运行时启用菜单栏项 [英] Menu Strip items enabling in runtime

查看:135
本文介绍了在运行时启用菜单栏项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的菜单条如下所示.
在加载时间,我想使启用和可见属性为真. 下面是我的代码,但是没有在打印选项下使用预览和打印选项.

My menu Strip is like below.
while loading time i want to make true for enable and visible property. Below is my code but that is not taking the preview and print option under the print option.

foreach (ToolStripMenuItem i in menuStrip.Items)
{                   
    for (int x = 0; x <= i.DropDownItems.Count-1; x++)
    {
        i.DropDownItems[x].Visible = true;
        i.DropDownItems[x].Enabled = true;
    }
    i.Available = true;
    i.Visible = true;
    i.Enabled = true;
}

推荐答案

我建议使用一些

  • 获取MenuStripToolStripContextMenuStripStatusStrip
  • 的所有后代(子代,子代...)
  • 获取某项的所有后代
  • 获取一个物品及其所有后代
    • Get all descendants (children, children of children, ...) fo a MenuStrip, ToolStrip or ContextMenuStrip or StatusStrip
    • Get all descendants of an item
    • Get an item and all of its descendants

    后代扩展方法

    以下扩展方法适用于MenuStripToolStripContextMenuStripStatusStrip:

    The following extension methods will work for a MenuStrip, ToolStrip, ContextMenuStrip or StatusStrip:

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    public static class ToolStripExtensions
    {
        public static IEnumerable<ToolStripItem> Descendants(this ToolStrip toolStrip)
        {
            return toolStrip.Items.Flatten();
        }
        public static IEnumerable<ToolStripItem> Descendants(this ToolStripDropDownItem item)
        {
            return item.DropDownItems.Flatten();
        }
        public static IEnumerable<ToolStripItem> DescendantsAndSelf (this ToolStripDropDownItem item)
        {
            return (new[] { item }).Concat(item.DropDownItems.Flatten());
        }
        private static IEnumerable<ToolStripItem> Flatten(this ToolStripItemCollection items)
        {
            foreach (ToolStripItem i in items)
            {
                yield return i;
                if (i is ToolStripDropDownItem)
                    foreach (ToolStripItem s in ((ToolStripDropDownItem)i).DropDownItems.Flatten())
                        yield return s;
            }
        }
    }
    

    示例

    • 禁用特定项目的所有后代:

    • Disable all descendants of a specific item:

    fileToolStripMenuItem.Descendants().ToList()
        .ForEach(x => {
            x.Enabled = false;
        });
    

  • 禁用菜单栏的所有后代:

  • Disable all descendants of the menu strip:

    menuStrip1.Descendants().ToList()
        .ForEach(x => {
            x.Enabled = false;
        });
    

  • 这篇关于在运行时启用菜单栏项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆