I am new here. So apologize in advance if not wording question properly. I am developing an application (VS2013, Visual Basic) that using multiple menu items in the MenuStrip. When item is clicked the identical function is called - the Tab is created and appropriate form is loaded. i.e.
Private Sub XXXMNU0039_Click(sender As Object, e As EventArgs) Handles XXXMNU0039.Click
Dim f1 As New frm_B05_01_SalesQuotes
Dim imgnm As String = "XXXMNU0039"
Call XXXTabPages("Sales Quotes", f1, imgnm)
End Sub
Private Sub XXXMNU0040_Click(sender As Object, e As EventArgs) Handles XXXMNU0040.Click
Dim f1 As New frm_B05_03_SalesQuotesReports
Dim imgnm As String = "XXXMNU_Reports"
Call XXXTabPages("Sales Quotes Reports", f1, imgnm)
End Sub
.... I am wondering is there is a way to create a "global" default "on click event" for all menu items that will accomplish the same thing by default. I have all the relevant information for each menu item stored in a table and hope to avoid creating "on click" for each item.
Thanks in advance.
You could have the same handler for the click event of your ToolStripMenuItems
.
Just add, after the first Handles XXXMNU0039.Click
the event to handle for another ToolStripMenuItem
and so on
Oviously, then problem is how to differentiate the various ToolStripMenuItem that calls the same event handler. But in the event arguments there is the Sender object that represent the current ToolStripMenuItem
that has called the event.
Just DirectCast to a ToolStripMenuItem
and read its name to pass the correct parameter to the XXXTablPages
method
Private Sub menuItemHandler_Click(sender As Object, e As EventArgs) _
Handles XXXMNU0039.Click, XXXMNU0040.Click
Dim f1 As New frm_B05_01_SalesQuotes
Dim itm = DirectCast(sender, ToolStripMenuItem)
Dim imgnm As String = item.Name
Call XXXTabPages("Sales Quotes", f1, imgnm)
End Sub