简体   繁体   English

我在工具条菜单项的项目检查中需要帮助

[英]i need help in item check in tool strip menu item

i got a column here..what i'm trying to do is when i select an item 我在这里有专栏..我想做的是当我选择一个项目时

在此处输入图片说明

i want the item in the context menu to be check according to the status in the column 我希望根据列中的状态检查上下文菜单中的项目

在此处输入图片说明

here's what im trying so far 这是我到目前为止正在尝试的

 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

but just gives me nothing..how can i turn this around ? 但是什么也没给我..我该如何扭转呢? been struggling with this since last night..tnx in advance 自昨晚以来一直在为此苦苦挣扎..tnx

Here are two possible solutions to your issue. 这是针对您的问题的两种可能的解决方案。 The first is based more on your original code, which I wasn't 100% sure which event you were targeting so I couldn't test it: 第一个更多地基于您的原始代码,我不能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

Next is the actual code that I used to test this functionality. 接下来是我用来测试此功能的实际代码。 I used the Opening event for the context menu to make this work. 我使用了上下文菜单的Opening事件来完成此工作。 This may not work for you if you are reusing the same context menu for different columns or controls but if not then I would recommend this approach: 如果您将相同的上下文菜单用于不同的列或控件,则这可能对您不起作用,但如果不这样,那么我建议采用这种方法:

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

In your original code you were only looping through the parent menu items. 在原始代码中,您仅循环浏览父菜单项。 In this updated code it looks for the parent item 'Status' and then loops through the child items to find the status you need to check. 在此更新的代码中,它将查找父项“状态”,然后循环遍历子项以查找您需要检查的状态。

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

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