MenuStrip 项目启用/禁用 vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29661171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:01:53  来源:igfitidea点击:

MenuStrip items Enable/Disable vb.net

vb.net

提问by Anel Hodzic

How to programaticaly Enable or Disable MenuStrip items.

如何以编程方式启用或禁用 MenuStrip 项目。

Example if i have this

例如,如果我有这个

enter image description here

在此处输入图片说明

I want to disable the item2 and item3. Tried with

我想禁用 item2 和 item3。试过

  MenuStrip1.Items("Item 1").Enabled = False
        MenuStrip1.Items(2).Enabled = False

采纳答案by ??ssa P?ngj?rdenlarp

Going by the image, it appears you want to disable/enable things in the dropdown.

通过图像,您似乎想要禁用/启用下拉列表中的内容。

Each top level menu item is itself an object which contains the actual drop down items - the MenuStrip is just a container for them. So, if I have a File | View | Toolsmenu, there will be three ToolStripMenuItems to work with, each with a DropDownItemscollection of those entries. So:

每个顶级菜单项本身就是一个包含实际下拉项的对象 - MenuStrip 只是它们的容器。所以,如果我有一个File | View | Tools菜单,将有三个ToolStripMenuItems 可以使用,每个都有DropDownItems这些条目的集合。所以:

ViewMenuItem.DropDownItems(2).Enabled = False

This disables the 3rd dropdown item on the View menu. Yours might be named ItemsToolStripMenuItem. The UI designer doesn't use a key to create/add new dropdown items, so the string overload wont work unless you are adding them manually:

这将禁用“查看”菜单上的第三个下拉项。你的可能会被命名为ItemsToolStripMenuItem。UI 设计器不使用键来创建/添加新的下拉项,因此除非您手动添加它们,否则字符串重载将不起作用:

' create new DD item
Dim foo = New ToolStripMenuItem("Foo", Nothing, 
              AddressOf FooToolStripMenuItem_Click, "Foo")
' add to menu
ViewMenuItem.DropDownItems.Add(foo)

' access by key
ViewMenuItem.DropDownItems("Foo").Enabled = True

回答by Tony

Just to add to this. I'm using VS Express 2013, and ViewMenuItem.DropDownItems(2).Enabled = Falsewouldn't work for me.

只是为了补充这一点。我正在使用 VS Express 2013,ViewMenuItem.DropDownItems(2).Enabled = False对我不起作用。

I found that this did.

我发现确实如此。

ShowRawDataToolStripMenuItem.Enabled = True

My menu name in this instance is "Show raw data" or "ShowRawData"

在这种情况下,我的菜单名称是“显示原始数据”或“ShowRawData”

I hope this helps somebody else.

我希望这对其他人有帮助。

回答by Mirko Tipka

I'm using:

我正在使用:

ContextMenuStrip1.Items.Item(1).Enabled = False
ContextMenuStrip1.Items.Item(2).Enabled = False