Switching mouse event handlers using a function

R

Ryan McFall

Hi:

I am trying to write a short program that demonstrates switching between
two different event handlers when a mouse click occurs. Here's the
relevant code:

public delegate void MouseHandler (object sender, MouseEventArgs args);

....

private void swap(MouseHandler old, MouseHandler new) {
simpleOpenGlControl1.MouseClick -= new MouseEventHandler (old);
simpleOpenGlControl1.MouseClick += new MouseEventHandler(new);

}

private void control_MouseClick(object sender, MouseEventArgs e) {
Console.WriteLine("Original Mouse Click");
swap(control_MouseClick, control_MouseClick2);
/*
control.MouseClick -= new MouseEventHandler(control_MouseClick);
control.MouseClick += new MouseEventHandler(control_MouseClick2);
*/
}

private void control_MouseClick2(object sender, MouseEventArgs e) {
Console.WriteLine("Modified Mouse Click");
swap(control_MouseClick2, control_MouseClick);
/*
control.MouseClick -= new MouseEventHandler(control_MouseClick2);
control.MouseClick += new MouseEventHandler(control_MouseClick);
*/
}


When I use the commented code in control_MouseClick and
control_MouseClick2, things work as expected; each new mouse click
prints a different message. But when I use the swap function, it seems
that removing the old mouse handler isn't working.

I'm fairly new to C#, so it's highly probable I'm misunderstanding
something fundamental here. Any insights would be greatly appreciated!

Thanks,
Ryan McFall
 
C

ClayB

In your swap routine, instead of

simpleOpenGlControl1.MouseClick -= new MouseEventHandler
(old);
simpleOpenGlControl1.MouseClick += new
MouseEventHandler(new);

try

simpleOpenGlControl1.MouseClick -= old;
simpleOpenGlControl1.MouseClick += new;

It worked for me in a little sample.

==================
Clay Burch
Syncfusion, Inc.
 
R

Ryan McFall

ClayB said:
In your swap routine, instead of

simpleOpenGlControl1.MouseClick -= new MouseEventHandler
(old);
simpleOpenGlControl1.MouseClick += new
MouseEventHandler(new);

try

simpleOpenGlControl1.MouseClick -= old;
simpleOpenGlControl1.MouseClick += new;

OK, this worked for me, if I changed the signature of swap to be:
swap (MouseEventHandler old, MouseEventHandler new)

I'm more concerned with understanding exactly what's going on than I am
getting it to work, so two questions remain in my mind:

1. Why did it work when the right hand side of += and -= were not
passed as parameters to a function (that is, when I didn't use swap)?
In either case I'm constructing a new MouseEventHandler, so it doesn't
seem to be a case of the -= operator looking for equality of object
references.

2. When I call your new version of swap, I'm doing:
swap (control_MouseClick, control_MouseClick2)
But control_MouseClick and control_MouseClick2 are not objects of type
MouseEventHandler (like the signature of swap should require), they are
methods. Why does this compile?

Thanks for any more insight anyone has on this issue.

Ryan
 
S

Stoitcho Goutsev \(100\)

Ryan,

Here I'm try to explain what is going wrong with your swap method (beside
the fact that *new* is a keywords and you cannot use it as an identifier
unless you don't prefix it with @ which I believe is not a good practice)

First let start what delegate is. When the compiler finds a deleagate
declaration it generates a class that wraps a pointer to the method and
defined several methods: Invoke, BeginInvoke and EndInvoke. In your case the
Invoke method is interesting. This method has the signature that you you
provided when declaring the event (only method with this signature can be
used with this delegate when you declare event handlers for example).
When you attach event handers you actually combine delegates by creating
list of delegates. when you remove an event handler the code in the delgate
class walks the list and removes the first delegate that have points to the
same method (it compares the adress of the method).

In your code you have:

1. In the swap method

simpleOpenGlControl1.MouseClick -= new MouseEventHandler (old);

2. In the mouse click event hander
control.MouseClick -= new MouseEventHandler(control_MouseClick);

In the swap *old* is delegate itself and the delegate the you are trying to
remove from the event handlers chaing actually points to the old's Invoke
method not to the event handler directly. That's why it doesn't find it in
the list and nothing gets removed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top