簡體   English   中英

我在工具條菜單項的項目檢查中需要幫助

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

我在這里有專欄..我想做的是當我選擇一個項目時

在此處輸入圖片說明

我希望根據列中的狀態檢查上下文菜單中的項目

在此處輸入圖片說明

這是我到目前為止正在嘗試的

 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

在原始代碼中,您僅循環瀏覽父菜單項。 在此更新的代碼中,它將查找父項“狀態”,然后循環遍歷子項以查找您需要檢查的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM