0

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.

Steve
  • 213,761
  • 22
  • 232
  • 286
goryef
  • 1,337
  • 3
  • 22
  • 37

1 Answers1

0

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
Steve
  • 213,761
  • 22
  • 232
  • 286
  • That's the step in a right direction for sure. Thanks a lot. I will try it. ideally, i would like to avoid to add "Handles ..." for each new item. – goryef Aug 12 '14 at 15:55
  • Forms designer, Properties Window, Events property grid (The lightning bolt icon) In any case, if you want an event to be handled by your code you should tell it to the compiler otherwise none will call you – Steve Aug 12 '14 at 15:56
  • Works perfectly. Exactly how i hoped it would. Thanks a lot – goryef Aug 12 '14 at 16:14