vb.nettoolstripitem

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

enter image description here

i want the item in the context menu to be check according to the status in the column

enter image description here

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


Solution

  • 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:

        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. 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.