2

如何禁用上下文菜单中包含分隔符项的所有菜单项?

我的做法:

    For Each item As ToolStripMenuItem In ContextMenuStrip1.Items
        item.Enabled = False
    Next

如果我在菜单中没有分隔符但使用分隔符我得到错误:

无法将“System.Windows.Forms.ToolStripSeparator”类型的对象转换为“System.Windows.Forms.ToolStripMenuItem”类型。

如何禁用菜单中包含分隔项的所有项目?

4

1 回答 1

2

代码示例(未经测试)来说明我的评论:

For i = 0 To ContextMenuStrip1.Items.Count - 1
    If TypeOf ContextMenuStrip1.Items(i) Is ToolStripMenuItem Then
        CType(ContextMenuStrip1.Items(i), ToolStripMenuItem).Enabled = False
    End If
Next

基本上,您会浏览菜单中的所有项目,如果当前项目是类型ToolStripMenuItem,则禁用它。

于 2013-08-10T06:41:38.387 回答