473,770 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s 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 SetControlEvent s()
{
foreach (Control c in control.Control s)
{
// for all controls
c.Enter += new System.EventHan dler(this.Contr ol_OnEnter);
}
}
// on form load call SetControlEvent s
}

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.EventHan dler(Control_On Enter(myControl ,
null));// This gives the compiler error 'Method name expected'

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

Nov 17 '05 #1
6 1623
> myControl.Enter -= new System.EventHan dler(Control_On Enter);// 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(Co ntrol_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.EventHan dler(Control_On Enter);// 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(Co ntrol_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.E nter -= new EventHandler(ba se.Control_OnEn ter);

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.EventArg s e)
{ //... }
//...
}

public class MyForm : BaseForm {

public override void Control_OnEnter (object sender, System.EventArg s e) {
if (sender == myTextBox) {
return;
}
base.Control_On Enter(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
1883
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. One of the main problems at this point lies in cancelling the event. I have found that the TAB fires the onkeypress in NN, but not in IE. I can cancel the onkeypress fine in NN. The TAB fires the onkeydown in IE and can be cancelled in IE. ...
3
1488
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 appearing after following steps listed in numerous posts in this group. I detect the onkeydown event of the body as follows: <body onkeydown="return bodyOnKeyPress(event);">
5
7594
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 fires. If, for example, the onBlur event was an alert() popup: when the user clicks the close window icon, the window closes and then the alert pops up. Clearly, if the user closed the window, there is no point to performing the onBlur event.
7
2779
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 with the last value it had before the user changed it. Pretty typical Now, the user types some invalid value and clicks OK. The user gets a message saying the value is not valid, and the bad value is replaced with the last good one. So far, so...
0
1742
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 GiveFeedback
1
211
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
16044
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 statement like as follows in the PageLoad: private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { //do something here } else {
9
4077
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 iframe. CODE ===== <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en_US"
3
1331
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 this: global MapPointMod MapPointMod = win32com.client.gencache.EnsureModule("{51C0A9CA-F7B7-4F5A-96F4-43927C6FA50F}", 0, 1, 0) class MyPanel(wx.Panel):
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10037
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.