469,945 Members | 2,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 5814
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by John Kirksey | last post: by
6 posts views Thread by crk2 | last post: by
reply views Thread by zeng.hui.stephen | last post: by
9 posts views Thread by jeff | last post: by
4 posts views Thread by SandyIsCool | last post: by
4 posts views Thread by Kimmo Laine | last post: by
22 posts views Thread by schneider | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.