Help with cancelling events

P

PromisedOyster

Hi

How do I cancel an event in a class where that event is setup in
another derived class. See example below.

Thanks


All our winforms are derived from one of our own classes, BaseForm.
This form does things like setup event handlers on each control so that
when you set focus to an edit box it selects the text.

// The base form that all our forms are derived from (abridged)
public class BaseForm : System.Windows.Forms.Form
{
public void Control_OnEnter(object sender, System.EventArgs e)
{
Control c = (Control)sender;
if (c is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox e =
(System.Windows.Forms.TextBox)c;
e.SelectAll();
}

private void SetControlEvents()
{
foreach (Control c in control.Controls)
{
// for all controls
c.Enter += new System.EventHandler(this.Control_OnEnter);
}
}
// on form load call SetControlEvents
}

Now we have another form that is derived from BaseForm. That form
contains a TextBox (myControl). In this class I want to somehow cancel
the event associated with myControl.

public class BaseForm : MyForm
{

myControl.Enter -= new System.EventHandler(Control_OnEnter(myControl,
null));// This gives the compiler error 'Method name expected'

myControl.Enter -= new System.EventHandler(Control_OnEnter);// This
compiles but Control_OnEnter in the base class is still called????


}
 
G

Guest

myControl.Enter -= new System.EventHandler(Control_OnEnter);// This
compiles but Control_OnEnter in the base class is still called????
You are working with two different EventHandlers. The one you hook up is a
different one than the one you create in the above snippet.

So, you can solve this with ease:

Hook up event:
EventHandler handler = new EventHandler(Control_OnEnter);
myControl.Enter += handler;

//Store handler as global variable.

Remove event:
myControl.Enter -= this.handler;

Regards Alexander
 
P

PromisedOyster

Alexander said:
You are working with two different EventHandlers. The one you hook up is a
different one than the one you create in the above snippet.

So, you can solve this with ease:

Hook up event:
EventHandler handler = new EventHandler(Control_OnEnter);
myControl.Enter += handler;

//Store handler as global variable.

Remove event:
myControl.Enter -= this.handler;

Regards Alexander

Thanks Alexander,

Your explanantion makes sense. However, within this base class, many
event handlers are setup. ie one for each control on the form. I guess
I could keep a collection of event handlers/control name, and get the
relevant handler but that would be messy. Alternatively, I could use a
derived control, but that seems like a lot of work.

Do you have any other bright ideas?

Can I say the following????

EventHandler handler = myControl.Enter;
myControl.Enter -= handler;

or perhaps even
myControl.Enter -= myControl.Enter;
 
G

Guest

EventHandler handler = myControl.Enter;
myControl.Enter -= handler;

or perhaps even
myControl.Enter -= myControl.Enter;
This is definitely not possible. The event is some sort of "collection" and
not a single EventHandler.

The only possiblity I see (at least on the fly) is to keep track of the
EventHandler you added.

Regards Alexander
 
G

Guest

Ok, I quickly built some classes representing your problem.

And if I call (from the sub class):

this.textBox1.Enter -= new EventHandler(base.Control_OnEnter);

everything works fine.
It seems to me that the event collection ensures that one method occurs only
once in the collection.

Are you sure that you remove the EventHandler from the proper Control?
 
G

Guest

alternatively, a "less-effort" approach like the following

public class BaseForm : System.Windows.Forms.Form
{
public virtual void Control_OnEnter(object sender, System.EventArgs e)
{ //... }
//...
}

public class MyForm : BaseForm {

public override void Control_OnEnter(object sender, System.EventArgs e) {
if (sender == myTextBox) {
return;
}
base.Control_OnEnter(sender,e);
}

//...
}

however, sometimes its not allowed to change the base class. hope this helps
if you do.
 
P

PromisedOyster

Sorry, I have found my problem.

The control I had was using had inner controls. I was setting the event
on the inner control but cancelling it on the outer control.
 

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