By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,207 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,207 IT Pros & Developers. It's quick & easy.

Switching mouse event handlers using a function

P: n/a
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
Feb 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.


Feb 1 '07 #2

P: n/a
ClayB wrote:
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
Feb 1 '07 #3

P: n/a
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.
--
HTH
Stoitcho Goutsev (100)

"Ryan McFall" <mc****@hope.eduwrote in message
news:eh*************@TK2MSFTNGP02.phx.gbl...
ClayB wrote:
>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

Feb 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.