Hi cj,
Thank you for your post.
I think to fully understand why the SubMenuItem_Click can handles multiple
menuitem's click event, you need to understand what is Delegate and what's
the relationship between Event and Delegate.
A delegate is a class that can hold a reference to a method. Unlike other
classes, a delegate class has a signature, and it can hold references only
to methods that match its signature. A delegate is thus equivalent to a
type-safe function pointer or a callback. A delegate declaration is
sufficient to define a delegate class, for example:
Public Delegate Sub EventHandler(sender as Object, e As EventArgs)
You will notice the parameter and return value matches your "Private sub
SunMenuItem_Click" which will handle the menu item click event. (The
default in Visual Basic is to pass arguments by value.)
An event is a message sent by an object to signal the occurrence of an
action. The action could be caused by user interaction, such as a mouse
clock, or it could be triggered by some other program logic. The object
that raises the event is called the event sender. The object that captures
the event and responds to it is called the vent receiver.
In event communication, the event sender class does not know which object
or method will receive (handle) the events it raises. What is needed is an
intermediary (or pointer-like mechanism) between the source and the
receiver. That's why Delegate come into play.
Custom event delegates are needed only when an event generates event data.
Many events, including the menuitem's click, do not generate event data. In
such situations, the event delegate provided in the class library for the
no-data event, System.EventHandler, is adequate. Since System.Object is the
ultimate base class of all classes in the .NET Framework, and you know that
your event handler will only handle the menu item's click event, you can
cast the sender to a strong type MenuItem. You will see its properties and
methods after you casted it to the strong type.
The Handles keyword and the AddHandler statement both allow you to specify
that particular procedures handle particular events, but there are
differences. The AddHandler statement connects procedures to events at run
time. Use the Handles keyword when defining a procedure to specify that it
handles a particular event.
Some referneces:
#Handling and Raising Events
http://msdn2.microsoft.com/en-us/library/edzehd2t.aspx
#Arguments Passing By Value and By Reference
http://msdn2.microsoft.com/en-us/library/ddck1z30.aspx
Hope this helps. Please feel free to post here if anything is unclear.
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.