[英]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:我建议使用一些扩展方法来:
MenuStrip
, ToolStrip
or ContextMenuStrip
or StatusStrip
MenuStrip
、 ToolStrip
或ContextMenuStrip
或StatusStrip
的所有后代(孩子、孩子的孩子……)Descendants Extension Methods后代扩展方法
The following extension methods will work for a MenuStrip
, ToolStrip
, ContextMenuStrip
or StatusStrip
:以下扩展方法适用于
MenuStrip
、 ToolStrip
、 ContextMenuStrip
或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;
}
}
}
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.