473,549 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Placing a event handler in the base class

Hi Everyone,

I have a few form classes that inherit from the same base class. The main
reason that this is done is that some event handlers are common between
these classes and I was trying to save time and not have to put a event
handler in every form class.

The base class has an event handler for Edit.Enter and Edit.Leave so that it
can turn the Edit menu items on and off.

So here is the class

public class BaseFormClass : Form
{
private Object curEditCtrl;
public BaseFormClass()
{
curEditCtrl = null;
}

protected void EditCtrl_Enter( object sender, EventArgs e)
{
curEditCtrl = sender;
//turn on the edit menu items (copy, cut, paste...)
}

protected void EditCtrl_Leave( object sender, EventArgs e)
{
curEditCtrl = null;
//turn off the edit menu items (copy, cut, paste...)
}
}

then I have a class ChildForm1 that inherits from BaseFormClass

public partial class ChildForm1 : BaseFormClass
{
public ChildForm1()
{
InitializeCompo nent();
}
};

In the InitializeCompo nent method there is a line

this.questionEd it.Enter += new System.EventHan dler(this.EditC trl_Enter);

where EditCtrl_Enter is defined in BaseFormClass.

Every this compiling fine and running fine, but I could no longer open
ChildForm1 in Design View because I would get this error:
"The Method 'EditCtrl_Enter ' cannot be the method for an event because a
class this class derives from already defines the method."

So I solved the problem by moving the event handler assignment line out of
InitializeCompo nent and put it in the constructor of the ChildForm1

public partial class ChildForm1 : BaseFormClass
{
public ChildForm1()
{
InitializeCompo nent();
this.questionEd it.Enter += new
System.EventHan dler(this.EditC trl_Enter);
}
};
This lets me open ChildForm1 in design view.

Can anyone think of a better solution for this problem?

With my solution the Events Property window does not show that Enter is
being handled.

Thanks
AliR.
Jul 15 '08 #1
1 3348
On Jul 15, 11:09*am, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
Hi Everyone,

I have a few form classes that inherit from the same base class. *The main
reason that this is done is that some event handlers are common between
these classes and I was trying to save time and not have to put a event
handler in every form class.

The base class has an event handler for Edit.Enter and Edit.Leave so thatit
can turn the Edit menu items on and off.

So here is the class

public class BaseFormClass : Form
{
* * private Object curEditCtrl;
* * public BaseFormClass()
* * {
* * * * curEditCtrl = null;
* * }

* * protected void EditCtrl_Enter( object sender, EventArgs e)
* * {
* * * * curEditCtrl = sender;
* * * * //turn on the edit menu items (copy, cut, paste...)
* * }

* *protected void EditCtrl_Leave( object sender, EventArgs e)
* *{
* * * * curEditCtrl = null;
* * * * //turn off the edit menu items (copy, cut, paste...)
* * }

}

then I have a class ChildForm1 that inherits from BaseFormClass

public partial class ChildForm1 : BaseFormClass
{
* * public ChildForm1()
* * {
* * * * InitializeCompo nent();
* * }

};

In the InitializeCompo nent method there is a line

this.questionEd it.Enter += new System.EventHan dler(this.EditC trl_Enter);

where EditCtrl_Enter is defined in BaseFormClass.

Every this compiling fine and running fine, but I could no longer open
ChildForm1 in Design View because I would get this error:
"The Method 'EditCtrl_Enter ' cannot be the method for an event because a
class this class derives from already defines the method."

So I solved the problem by moving the event handler assignment line out of
InitializeCompo nent and put it in the constructor of the ChildForm1

public partial class ChildForm1 : BaseFormClass
{
* * public ChildForm1()
* * {
* * * * InitializeCompo nent();
* * * * this.questionEd it.Enter += new
System.EventHan dler(this.EditC trl_Enter);
* * }

};

This lets me open ChildForm1 in design view.

Can anyone think of a better solution for this problem?

With my solution the Events Property window does not show that Enter is
being handled.

Thanks
AliR.
Hi,

I think this could be an issue with the IDE, it expect to find the
method defined in the class itselft, not in a parent class.
You could move the handler assignation from the InitializeCompo nent
to the constructor. there the IDE will not see it and will not
complain.
Of corse you will not see the event being handle in the IDE
Jul 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6079
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an...
1
11545
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at http://weblogs.asp.net/asmith/archive/2003/09/15/27684.aspx but it was far more complex then I needed. (I got lost trying to figure it all...
13
3486
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through...
0
2938
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it such as when they do a delete. Everything is great. By and large it will be used in my repeater controls using the command event when the user...
0
1846
by: Patrick Lioi | last post by:
We have form that is used as the base class of all of our forms, let's call it BaseApplicationForm. We have another form, say ChildApplicationForm that inherits from BaseApplicationForm. The child form implements an event handler for the Resize event. Inside BaseApplicationForm.InitializeComponent, this.ClientSize is set to a value. On...
9
2458
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4...
3
1812
by: polocar | last post by:
Hi, I'm writing a C# program (using Visual Studio 2005 Professional Edition). I have defined a class MyPanel in the following way: class MyPanel : Panel { ... }
3
2161
by: =?Utf-8?B?RWR3aW4=?= | last post by:
Hello Everyone. Below is the code that is in question. --- BEGIN CODE WITHIN INHERITED FORM --- protected virtual void OnFormClosing(object sender, FormClosingEventArgs e) { switch (e.CloseReason) { case CloseReason.UserClosing: if (this.AskUserIfOkayToClose() == true) { this.ExecuteApplicationCloseProcedures(); }
3
1363
by: Tony | last post by:
Hello! Is it the normal procedure in C# and .NET framework to always use the actual event object which is passed as the second parameters to the event handler. All of them are derived from the base class which is EventArgs so because of this the second parameters could in all cases be EventArgs which had be be cased in the most cases to...
0
7520
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...
0
7450
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...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7470
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...
1
5368
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...
0
3500
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...
1
1941
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
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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...

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.