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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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,

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.
 
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]
- (e-mail address removed)

Robert Werner said:
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.
 
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
 
Back
Top