我需要帮助检查工具条菜单项中的项目
i need help in item check in tool strip menu item
我在这里有一个专栏..我想做的是当我 select 一个项目
我希望根据列中的状态检查上下文菜单中的项目
这是我目前所做的尝试
Dim currentItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim parentItem = DirectCast(currentItem.OwnerItem, ToolStripMenuItem)
For Each ctl As ToolStripMenuItem In parentItem.DropDownItems
If TypeOf ctl Is ToolStripMenuItem Then
If ctl.Text = ListView1.SelectedItems.Item(0).Text Then
currentItem = DirectCast(ctl, ToolStripMenuItem)
currentItem.Checked = True
End If
End If
Next
但什么也没给我..我该如何扭转局面?从昨晚开始就一直在为此苦苦挣扎..提前tnx
针对您的问题,有两种可能的解决方案。第一个更多地基于您的原始代码,我不是 100% 确定您针对的是哪个事件,所以我无法测试它:
Dim currentItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim parentItem = DirectCast(currentItem.OwnerItem, ToolStripMenuItem)
For Each ctl As ToolStripMenuItem In parentItem.DropDownItems
If ctl.Text = "Status" Then
For Each dropctl As ToolStripMenuItem In ctl.DropDownItems
If dropctl.Text = ListView1.SelectedItems.Item(0).Text Then
dropctl.Checked = True
Else
dropctl.Checked = False ' Ensure that you uncheck a previously checked status
End If
Next
End If
Next
接下来是我用来测试此功能的实际代码。我使用上下文菜单的 Opening 事件来完成这项工作。如果您为不同的列或控件重复使用相同的上下文菜单,这可能对您不起作用,但如果不是,那么我会推荐这种方法:
Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
If ListView1.SelectedItems.Count > 0 Then
For Each ctl As ToolStripMenuItem In CType(sender, System.Windows.Forms.ContextMenuStrip).Items
If ctl.Text = "Status" Then
For Each dropctl As ToolStripMenuItem In ctl.DropDownItems
If dropctl.Text = ListView1.SelectedItems.Item(0).Text Then
dropctl.Checked = True
Else
dropctl.Checked = False ' Ensure that you uncheck a previously checked status
End If
Next
End If
Next
Else
e.Cancel = True ' Don't show the context menu if no row was clicked on
End If
End Sub
在您的原始代码中,您只是循环访问父菜单项。在此更新的代码中,它查找父项 'Status',然后遍历子项以查找您需要检查的状态。
我在这里有一个专栏..我想做的是当我 select 一个项目
我希望根据列中的状态检查上下文菜单中的项目
这是我目前所做的尝试
Dim currentItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim parentItem = DirectCast(currentItem.OwnerItem, ToolStripMenuItem)
For Each ctl As ToolStripMenuItem In parentItem.DropDownItems
If TypeOf ctl Is ToolStripMenuItem Then
If ctl.Text = ListView1.SelectedItems.Item(0).Text Then
currentItem = DirectCast(ctl, ToolStripMenuItem)
currentItem.Checked = True
End If
End If
Next
但什么也没给我..我该如何扭转局面?从昨晚开始就一直在为此苦苦挣扎..提前tnx
针对您的问题,有两种可能的解决方案。第一个更多地基于您的原始代码,我不是 100% 确定您针对的是哪个事件,所以我无法测试它:
Dim currentItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim parentItem = DirectCast(currentItem.OwnerItem, ToolStripMenuItem)
For Each ctl As ToolStripMenuItem In parentItem.DropDownItems
If ctl.Text = "Status" Then
For Each dropctl As ToolStripMenuItem In ctl.DropDownItems
If dropctl.Text = ListView1.SelectedItems.Item(0).Text Then
dropctl.Checked = True
Else
dropctl.Checked = False ' Ensure that you uncheck a previously checked status
End If
Next
End If
Next
接下来是我用来测试此功能的实际代码。我使用上下文菜单的 Opening 事件来完成这项工作。如果您为不同的列或控件重复使用相同的上下文菜单,这可能对您不起作用,但如果不是,那么我会推荐这种方法:
Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
If ListView1.SelectedItems.Count > 0 Then
For Each ctl As ToolStripMenuItem In CType(sender, System.Windows.Forms.ContextMenuStrip).Items
If ctl.Text = "Status" Then
For Each dropctl As ToolStripMenuItem In ctl.DropDownItems
If dropctl.Text = ListView1.SelectedItems.Item(0).Text Then
dropctl.Checked = True
Else
dropctl.Checked = False ' Ensure that you uncheck a previously checked status
End If
Next
End If
Next
Else
e.Cancel = True ' Don't show the context menu if no row was clicked on
End If
End Sub
在您的原始代码中,您只是循环访问父菜单项。在此更新的代码中,它查找父项 'Status',然后遍历子项以查找您需要检查的状态。