避免tooltripmenu关闭所选项目

时间:2016-02-22 01:06:15

标签: vb.net contextmenu contextmenustrip toolstripmenu

我在vb.net上有一个系统托盘应用程序。这个应用程序有一个菜单有几个选项。可以选择语言应用程序。这个语言选项有三个子菜单:英语,法语和西班牙语。

以下是三个子菜单的snipet代码:

submnuEnglish = New ToolStripMenuItem(rm.GetString("ContextMenusToolStripMenuItemLangEnglish"))
submnuEnglish.Image = My.Resources.en
submnuEnglish.CheckOnClick = True

submnuSpanish = New ToolStripMenuItem(rm.GetString("ContextMenusToolStripMenuItemLangSpanish"))
submnuSpanish.Image = My.Resources.es
submnuSpanish.CheckOnClick = True

submnuFrench = New ToolStripMenuItem(rm.GetString("ContextMenusToolStripMenuItemLangFrench"))
submnuFrench .Image = My.Resources.fr
submnuFrench .CheckOnClick = True

在rutime中,每次从语言选项菜单中选择语言时,应用程序主菜单会在选择后自动关闭,因此我需要右键单击系统托盘应用程序再次访问应用程序主菜单。我希望能够在没有语言子菜单的情况下选择语言/主应用菜单在每个选项上都会关闭。

我该怎么做?

解决方案 - 更新: mngLanguage下面对应一个ToolStripMenuItem对象,该对象包含类型为ToolStripMenuItem的子选项submnuEnglish,submnuSpanish和submnuFrench。

MainMenu是ContextMenuStrip类型的应用程序主菜单。

Private Sub ToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    ' uncheck the old ones
    Dim selectedLanguage As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
    For Each language As ToolStripMenuItem In mnuLanguage.DropDownItems
        If Not Object.Equals(selectedLanguage, language) Then
            language.CheckState = CheckState.Unchecked
        End If
    Next

    ' check the new one
    selectedLanguage.CheckState = CheckState.Checked

    ' Prevents language menu from being hiding
    ' Thanks to Crush Sundae ;) that guide me in the right direction with his comment
    Me.mnuLanguage.DropDown.Show()
End Sub

Private Sub MainMenu_Closing(ByVal sender As Object, _
                             ByVal e As System.Windows.Forms.ToolStripDropDownClosingEventArgs) _
Handles MainMenu.Closing

    ' Avoid main menu for closing on language selection
    If submnuEnglish.Pressed Or _
       submnuSpanish.Pressed Or _
       submnuFrench.Pressed Then

        e.Cancel = True
    End If

End Sub

需要在事件下方刷新所点击的语言项目的UI。如果没有实现,那么当选中一个项目(语言)时,移动鼠标后不会取消选中前一项。

Private Sub mnuLanguage_DropDownItemClicked( _
    ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) _
Handles mnuLanguage.DropDownItemClicked

    ' Refresh language selection on dropdown menu
    If e.ClickedItem.Equals(submnuEnglish) Then
        ' English language selected
        submnuSpanish.Checked = False
        submnuFrench.Checked = False
    ElseIf e.ClickedItem.Equals(submnuSpanish) Then
        ' Spanish language selected
        submnuEnglish.Checked = False
        submnuFrench.Checked = False
    Else
        ' French language selected
        submnuEnglish.Checked = False
        submnuSpanish.Checked = False
    End If

End Sub

通过以上实施,它就像一个魅力!

在实施上述解决方案之前,我试图使用以下方法解决它:

mnuLanguage.DropDown.AutoClose = True or False depending on the case

但这样做是因为UI行为异常,奇怪的行为,例如,带有子菜单的菜单没有在鼠标悬停时自动下拉。从我的观点来看,不建议使用它。

1 个答案:

答案 0 :(得分:1)

点击后再次打开它。

由于我无法从您的代码中看到更多内容,因此请使用this作为我所说内容的参考。

Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
     Me.ToolStripDropDownButton1.ShowDropDown()
EndSub