about removing an event handler

T

Tony Johansson

Hi!

If I have this event handler pictureBox_DragEnter below and the statement
that couple the event to the eventhandler
panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);

I wonder if there is an easy way to remove these two.
When I want to remove these two I follow this procedure today.
1. In the event property I click reset on the event that I want to remove.
2. I remove the event handler manually.
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
string filename;
if (((validData = GetFilename(out filename, e)) == true) &&
(e.Data.GetDataPresent(DataFormats.FileDrop)))
{
myImage = new Bitmap(filename);
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}

this.panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);

//Tony
 
F

Family Tree Mike

Hi!

If I have this event handler pictureBox_DragEnter below and the statement
that couple the event to the eventhandler
panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);

I wonder if there is an easy way to remove these two.
When I want to remove these two I follow this procedure today.
1. In the event property I click reset on the event that I want to remove.
2. I remove the event handler manually.
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
string filename;
if (((validData = GetFilename(out filename, e)) == true)&&
(e.Data.GetDataPresent(DataFormats.FileDrop)))
{
myImage = new Bitmap(filename);
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}

this.panel1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);

//Tony

I don't know why you would add this handler twice, but to remove, you
simply do:

panel1.DragEnter -= new DragEventHandler(pictureBox_DragEnter);
 
T

Tony Johansson

Family Tree Mike said:
I don't know why you would add this handler twice, but to remove, you
simply do:

panel1.DragEnter -= new DragEventHandler(pictureBox_DragEnter);

Yes but I want to remove both from the development environment without
adding any code.
I just want to remove both the event handler and the
panel1.DragEnter -= new DragEventHandler(pictureBox_DragEnter);

So as I mentioned to you I can remove this panel1.DragEnter += new
DragEventHandler(pictureBox_DragEnter);
by clicking on the reset for the event that I want to be reset.

But if I want to remove the actual event handler is the only way to manually
remove it ?

//Tony
 
P

Peter Duniho

Tony said:
Yes but I want to remove both from the development environment without
adding any code.
I just want to remove both the event handler and the
panel1.DragEnter -= new DragEventHandler(pictureBox_DragEnter);

So as I mentioned to you I can remove this panel1.DragEnter += new
DragEventHandler(pictureBox_DragEnter);
by clicking on the reset for the event that I want to be reset.

But if I want to remove the actual event handler is the only way to manually
remove it ?

Deleting the entire method should also remove subscriptions to it in the
Designer. At least with VS 2008 and 2010, that's what I see happen.

Other than that, no…there's not a single "remove the method and event
subscription at once" UI command.

So, just do only step #2 in your procedure. Skip the first part, and it
should work fine.

Pete
 

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