473,386 Members | 1,621 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,386 software developers and data experts.

How do I hook into the Load event of the base Control class?

I want to write one general purpose set of code that will handle the
population (where required) of all controls on a form. Though I'm new to C#
my instinct is to somehow hook into the Load event of the Control class, as
this should [hopefully] get inherited down to all of the controls.

Two questions:

1. Is this possible?

2. What's the simplest, most elegant way to do it?

Robert W.
MWTech
Nov 17 '05 #1
6 5998
Hi Robert,

Yes it is possible. The base Control class exposes a Load event:
Control.Load += new EventHandler (Control_Load);

You can find more at
http://msdn.microsoft.com/library/de...sloadtopic.asp

Cheers,
Steve Goodyear
Vancouver, Canada
Nov 17 '05 #2
Steve,

Greetings back from Vancouver (Kitsilano)!

I tried what you said, adding "Control.Load += new EventHandler
(Control_Load);" to the form's constructor but my code wouldn't compile,
giving me this error:

'System.Windows.Forms.Control' does not contain a definition for 'Load'
Then I modified your code to be more like in the link you gave me: "Load +=
new System.EventHandler (Control_Load);"

And I constructed this simple class:

private void Control_Load(object sender, System.EventArgs e)
{
MessageBox.Show("Loading control: " + sender.GetType().Name);
}
But when I ran this, the only control that appeared to execute this class
was the form itself, but none of its children.

Once again, what I'd ideally like to do would be to add one Load event for
the Control class, which will automatically be inherited by all of the
controls on the form, not just the form itself.

What am I doing wrong?

Robert W.
MWTech
www.mwtech.com

"Steve Goodyear" wrote:
Hi Robert,

Yes it is possible. The base Control class exposes a Load event:
Control.Load += new EventHandler (Control_Load);

You can find more at:
http://msdn.microsoft.com/library/de...sloadtopic.asp

Cheers,
Steve Goodyear
Vancouver, Canada

Nov 17 '05 #3
Robert,

The Control class does not have a Load event, only the Form. What you
will have to do is traverse the heiarchy of controls, and when you get to a
parent that is a form, you can hook into that Load event.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Robert Werner" <Ro**********@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
Steve,

Greetings back from Vancouver (Kitsilano)!

I tried what you said, adding "Control.Load += new EventHandler
(Control_Load);" to the form's constructor but my code wouldn't compile,
giving me this error:

'System.Windows.Forms.Control' does not contain a definition for 'Load'
Then I modified your code to be more like in the link you gave me: "Load
+=
new System.EventHandler (Control_Load);"

And I constructed this simple class:

private void Control_Load(object sender, System.EventArgs e)
{
MessageBox.Show("Loading control: " + sender.GetType().Name);
}
But when I ran this, the only control that appeared to execute this class
was the form itself, but none of its children.

Once again, what I'd ideally like to do would be to add one Load event for
the Control class, which will automatically be inherited by all of the
controls on the form, not just the form itself.

What am I doing wrong?

Robert W.
MWTech
www.mwtech.com

"Steve Goodyear" wrote:
Hi Robert,

Yes it is possible. The base Control class exposes a Load event:
Control.Load += new EventHandler (Control_Load);

You can find more at:
http://msdn.microsoft.com/library/de...sloadtopic.asp

Cheers,
Steve Goodyear
Vancouver, Canada

Nov 17 '05 #4
Nicholas,

Two questions:

1. Can I take the shortcut and just add my code directly into the form's
load event?

2. If I do this (or follow your more lengthy procedure), are you saying that
the event code will automatically cascade down to every control sitting on
the form?

Robert W.


"Nicholas Paldino [.NET/C# MVP]" wrote:
Robert,

The Control class does not have a Load event, only the Form. What you
will have to do is traverse the heiarchy of controls, and when you get to a
parent that is a form, you can hook into that Load event.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


Nov 17 '05 #5
Robert,

Yes, you can take the shortcut and just add the code to a handler for
the forms load event. However, it would require you to make changes to that
code every time you changed one of the controls on the form.

You could make it much easier by creating a base class which attaches to
the event, where the event handler is a virtual function that you can
override in any class that extends that base class. This would allow for
better encapsulation of code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Robert Werner" <Ro**********@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
Nicholas,

Two questions:

1. Can I take the shortcut and just add my code directly into the form's
load event?

2. If I do this (or follow your more lengthy procedure), are you saying
that
the event code will automatically cascade down to every control sitting on
the form?

Robert W.


"Nicholas Paldino [.NET/C# MVP]" wrote:
Robert,

The Control class does not have a Load event, only the Form. What
you
will have to do is traverse the heiarchy of controls, and when you get to
a
parent that is a form, you can hook into that Load event.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Nov 17 '05 #6
Hi Robert,

Small world indeed - I'm right across the bridge from you, downtown!

My appologies, I didn't give you enough details. Control is a base class
that all other controls are derived from in ASP.NET including the Page class
its self. One of the events inside that base class is Load (dispite Nicholas'
doubts - it really is there)which would fire for each control. Since Control
is the base class my example wouldn't be that clear. You want to reference it
like [Name of Control].Load += ... something like textbox1.Load += ... Does
that help make sense of what I meant?

If I understand your goal correctly you want to capture the event of each
control on the page as they load, is that right? One way you can make that
happen is using a foreach loop:

foreach (Control ctrl in Page.Controls)
{
ctrl.Load += new EventHandler (Control_Load);
}

....

private void Control_Load(object sender, System.EventArgs e)
{
MessageBox.Show("Loading control: " + sender.GetType().Name);
// you could even try to cast the sender to a Control
// type to get even more useful info:
Control ctrl = sender as Control;
if (ctrl != null)
MessageBox.Show("The Control ID: " + ctrl.ID);
}

You should place that foreach loop before Page_Load, like in Page_Init, if
it doesn't respond correctly.

Hope that's a little more helpful!

Cheers,
Steve Goodyear
Vancouver, Canada
Nov 17 '05 #7

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

Similar topics

9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
1
by: WFB | last post by:
Hi, I have a base class from which all of my pages derive (ABCBasePage). For example, ABCCustomerSelect Inherits ABCPasePage. I would now like to have ABCPocketSelect which should inherit from...
2
by: Sam | last post by:
I have a custom control (MyTextBox - taken from Microsoft website) that implements the IPostBackDataHandler interface. It is added to the controls collection of a placeholder control during the...
6
by: crk2 | last post by:
Here a simple one. (At least I think it is?) and any help would be truly appreciated. I have an inherited textbox on my form based on a custom texbox control. It looks something like this ...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
9
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...
4
by: SandyIsCool | last post by:
Hi, I am newbie to asp.net. I have small doubt regarding page life cycle. MSDN documentaion says that init event does blhah, blah load event does blah blah etc. But Id ont see any init or load...
4
by: Kimmo Laine | last post by:
Hi! Is there a way to generate Load-event for form without showing it? My problem is that if i try to set control states before Load is raised, control states may or may not work. Here is some...
22
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.