I am attempting to share a sub menu among several different parts of an application so that it is consistent (not wanting to copy/paste.) I thought it would be simple. I am doing something like this:
public class MyClientClass
Private WithEvents ctx As ContextMenuManager = New ContextMenuManager
Private Sub handler() Handles ctx._myToolstripMenuItem.Click
' do something useful
End Sub
End Class
Public Class ContextMenuManager
Public WithEvents _myToolstripMenuItem As ToolStripMenuItem
Public Sub New()
Me._myToolstripMenuItem = New ToolStripMenuItem
Me._myToolstripMenuItem.Name = "DoSomehting"
Me._myToolstripMenuItem.Size = New System.Drawing.Size(48, 20)
Me._myToolstripMenuItem.Text = "Go!"
End Sub
End Class
But that code gives me an Error: 'Handles' in classes must specify a 'WithEvents' variable, 'MyBase', 'MyClass' or 'Me' qualified with a single identifier.
I supplied the WithEvents keyword for both the container app and the menu item. What gives? What am I missing? Would a C# example give the same error?
Changed focus to solve this another way. Handling all events in the shared class and firing a common event with a common obj:
Private Sub MenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles ToolStripMenuItem.Click
' edit _legacyAttributes instance per specifics
' ...
' then always raise the same event with the updated payload object
RaiseEvent TypeChanged(_legacyAttributes)
End Sub
So, you can imagine that there are dozens of handlers like this in the class, each one modifying its _legacyAttributes as it makes sense...