M
Martijn Mulder
There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use
MouseEnter+=OnMouseEnter;
or
MouseEnter+=new System.EventHandler(OnMouseHandler);
Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other. What
is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void OnMouseLeave(System.Object
a,System.EventArgs b) { label.Text="MouseLeft"; } //Main [System.STAThread]
static void Main() { System.Windows.Forms.Application.Run(new Form()); }}
MouseEnter-event, you can use
MouseEnter+=OnMouseEnter;
or
MouseEnter+=new System.EventHandler(OnMouseHandler);
Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other. What
is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void OnMouseLeave(System.Object
a,System.EventArgs b) { label.Text="MouseLeft"; } //Main [System.STAThread]
static void Main() { System.Windows.Forms.Application.Run(new Form()); }}