473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 1610
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul THompson | last post by:
I have been working for some time to 1) detect tab and shift tab events 2) control the focus on the basis of these events. I have found that I can do this, but continue to have nagging problems. ...
3
by: mgw | last post by:
Hi, I'm trying to cancel the Shift+F3 key combination in Netscape 7.2 from performing the F3 key's default action of opening the Find dialog. I'm unable thus far to prevent the Find dialog from...
5
by: Dave Hammond | last post by:
Hi All, I have a web form which performs certain actions upon moving focus away from a field. However, if the user clicks the top corner 'X' icon to close the window, the onBlur event still...
7
by: Marina | last post by:
Imagine a form with some fields and an OK buttons that saves the information. Each field has validation logic in the Validating event. If the input is not valid, the control's value is replaced...
0
by: Baseman | last post by:
I am trying to find out how to cancel all TreeView events if an exception occurs during any one of the following... MouseDown BeforeSelect AfterSelect DragDrop DragEnter DragOver...
1
by: Vik | last post by:
Is it possible to cancel all the events or a specific event programmatically (for example, in a Page_Load event)? In other words, is it possible to stop a page code execution? Thank you.
7
by: Amadelle | last post by:
Hi all and thanks in advance, I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if...
9
by: pvsundarram | last post by:
hey, i am trying to cancel the keydown event for certain keycodes( for eg:- enter key ).But the cancelling of this event is not happening in firefox. Is there any way to cancel the event in the...
3
by: Oliver Nelson | last post by:
I have MapPoint working in Python, and I'm trying to cancel events on the map, but I can't seem to make that happen. I'm responding to the events successfully in my panel object. My code is like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.