简体   繁体   English

在运行时启用的菜单条项目

[英]Menu Strip items enabling in runtime

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;
}

I'd suggest using some Extension Methods to:我建议使用一些扩展方法来:

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

Descendants Extension Methods后代扩展方法

The following extension methods will work for a MenuStrip , ToolStrip , ContextMenuStrip or StatusStrip :以下扩展方法适用于MenuStripToolStripContextMenuStripStatusStrip

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;
        }
    }
}

Example例子

  • 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; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM