469,604 Members | 2,377 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

Help with cancelling events

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????
}

Nov 17 '05 #1
6 1449
> 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
Nov 17 '05 #2

Alexander Wehrli wrote:
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


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;

Nov 17 '05 #3
> 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
Nov 17 '05 #4
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?
Nov 17 '05 #5
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.
Nov 17 '05 #6
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.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Paul THompson | last post: by
5 posts views Thread by Dave Hammond | last post: by
1 post views Thread by Vik | last post: by
3 posts views Thread by Oliver Nelson | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.