2016-02-22 62 views
0

我在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,每個我選擇從語言選項菜單中選擇語言時,應用程序主菜單選擇後自動關閉,所以我需要做右鍵單擊系統托盤應用程序以再次訪問應用程序主菜單。我希望能夠選擇語言,而不必在每次選擇時關閉語言子菜單/主應用程序菜單。

我該怎麼做?

SOLUTION - 更新: 下面mngLanguage對應於包含類型ToolStripMenuItem選項submnuEnglish,submnuSpanish和submnuFrench一個ToolStripMenuItem對象。

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 

下面的事件是必要的,以刷新點擊語言項目的用戶界面。如果它沒有實現,那麼當一個項目被選中(語言)時,在移動鼠標後,前一個項目不會被取消選中。

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

單擊它後再次打開它。

由於我看不到更多的代碼,只需使用this作爲我所說的參考。

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

我已更新我的文章。我通過實施其他活動解決了這個問題,其中一個就是你說的。謝謝! – user1624552

+0

我很高興它有幫助。 :) –