patternMinor
Handle multiple menu click events in one Sub
Viewed 0 times
clickeventshandlesubmenuonemultiple
Problem
I have multiple menus that will change languages in one Sub that will handle multiple events of the menus click, It is working for me right now, I'm just wondering if there is better way to do it... I'm using the menus name to call the process for each menus.
Private Shared Sub CheckMenuItem(ByVal mnu As ToolStripMenuItem, ByVal checked_item As ToolStripMenuItem)
For Each menu_item As ToolStripMenuItem In mnu.DropDownItems.OfType(Of ToolStripMenuItem)()
menu_item.Checked = (menu_item Is checked_item)
Next
End Sub
Private Sub mnuEnglish_Click(sender As System.Object, e As EventArgs) _
Handles mnuEnglish.Click, mnuFrench.Click, mnuDutch.Click, _
mnuGerman.Click,mnuCroatian.Click, mnuCzech.Click, mnuHungarian.Click, _
mnuIndonesian.Click, mnuItalian.Click, mnuPolish.Click, mnuSlovak.Click, _
mnuSpanish.Click, mnuSwedish.Click, mnuTurkish.Click, mnuVietnamese.Click
Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
CheckMenuItem(mnuLanguage, item)
Select Case item.Name
Case "mnuEnglish"
MsgBox("english")
Case "mnuFrench"
MsgBox("French")
Case Else
MsgBox("Other Languages")
End Select
End SubSolution
See this Stack Overflow answer, where Cody Gray explains the syntax for the two ways of registering event handlers in vb.net:
The first involves the use of the Handles keyword, which you append to the end of the event handler method's definition. [...] The second involves the explicit use of the AddHandler statement, just like += in C#.
This means instead of statically declaring all the handled events with a
Or even better, loop through the menu items in that menu object, and assing all
The first involves the use of the Handles keyword, which you append to the end of the event handler method's definition. [...] The second involves the explicit use of the AddHandler statement, just like += in C#.
This means instead of statically declaring all the handled events with a
Handles keyword, you write code to add the handlers yourself:Public Sub New()
InitializeComponents()
AddHandler mnuEnglish.Click, AdressOf mnuEnglish_Click
AddHandler mnuFrench.Click, AdressOf mnuEnglish_Click
'...
End SubOr even better, loop through the menu items in that menu object, and assing all
Click events the AdressOf mnuEnglish_Click, so you won't have the opportunity to forget updating that piece of code when you add a new language / menu item in the designer:Public Sub New()
InitializeComponents()
ForEach item As ToolStripMenuItem In mnu.DropDownItems.OfType(Of ToolStripMenuItem)()
AddHandler item.Click, AdressOf mnuEnglish_Click
Next
End SubCode Snippets
Public Sub New()
InitializeComponents()
AddHandler mnuEnglish.Click, AdressOf mnuEnglish_Click
AddHandler mnuFrench.Click, AdressOf mnuEnglish_Click
'...
End SubPublic Sub New()
InitializeComponents()
ForEach item As ToolStripMenuItem In mnu.DropDownItems.OfType(Of ToolStripMenuItem)()
AddHandler item.Click, AdressOf mnuEnglish_Click
Next
End SubContext
StackExchange Code Review Q#44987, answer score: 5
Revisions (0)
No revisions yet.