避免在所选项目上关闭工具条菜单
Avoid toolstripmenu closed on item selected
我在 vb.net 上有一个系统托盘应用程序。这个应用程序有一个包含多个选项的菜单。 select 语言应用程序有一个选项。此语言选项有三个子菜单:英语、法语和西班牙语。
下面是三个子菜单的代码片段:
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 中,每次我从语言选项菜单 select 一种语言时,应用程序主菜单会在 selection 之后自动关闭,所以我需要右键单击系统托盘应用程序再次访问应用程序主菜单。我希望能够 select 没有语言的语言 submenu/main 应用程序菜单在每个 selection 上关闭。
我该怎么做?
解决方案 - 已更新:
mngLanguage 下面对应一个 ToolStripMenuItem 对象,它包含 ToolStripMenuItem 类型的选项 submnuEnglish、submnuSpanish 和 submnuFrench。
MainMenu是类型为ContextMenuStrip的app主菜单。
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 行为异常,行为怪异,例如,带有子菜单的菜单不会在鼠标悬停时自动下拉。个人观点不推荐使用
点击后重新打开
由于我无法从您的代码中看到更多信息,请使用 this 作为我所说内容的参考。
Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
Me.ToolStripDropDownButton1.ShowDropDown()
EndSub
我在 vb.net 上有一个系统托盘应用程序。这个应用程序有一个包含多个选项的菜单。 select 语言应用程序有一个选项。此语言选项有三个子菜单:英语、法语和西班牙语。
下面是三个子菜单的代码片段:
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 中,每次我从语言选项菜单 select 一种语言时,应用程序主菜单会在 selection 之后自动关闭,所以我需要右键单击系统托盘应用程序再次访问应用程序主菜单。我希望能够 select 没有语言的语言 submenu/main 应用程序菜单在每个 selection 上关闭。
我该怎么做?
解决方案 - 已更新: mngLanguage 下面对应一个 ToolStripMenuItem 对象,它包含 ToolStripMenuItem 类型的选项 submnuEnglish、submnuSpanish 和 submnuFrench。
MainMenu是类型为ContextMenuStrip的app主菜单。
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 行为异常,行为怪异,例如,带有子菜单的菜单不会在鼠标悬停时自动下拉。个人观点不推荐使用
点击后重新打开
由于我无法从您的代码中看到更多信息,请使用 this 作为我所说内容的参考。
Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
Me.ToolStripDropDownButton1.ShowDropDown()
EndSub