views:

347

answers:

2

To a WinForms control, I would like to add a handler after the container has initialized the control (or even better, after the parent has initialized all contained controls).

Reason: The custom control has an option to trigger an action automatically. It should also trigger when this option is first enabled.

However, at this point, other properties (like event handlers) are not yet wired up correctly, and thus the effect is not as expected.

Is this possible?

[edit]

I understand that this sounds "a bit unusual", so I want to clarify:

It is intended for this component: roughly, adding an option to "automatically create new document".

I can create the DocumentInfo itself during initialization, or when the property is assigned during initialization. However, at this point I should also fire the event to tell (usually the parent form) that it needs to update the UI. The event might not yet been wired up, or other controls arestill uninitialized, that's why I'd like to delay the event until the parent form is completely initialized.

+1  A: 

Have you tried wiring up to the form's Load event and doing the work there? (note this fires up every time you show the form...)

Dan C.
"Show" as in .Visible = false / .Visible = true?
peterchen
Yep. If you want it to run only once when the Form object is created, put it in the Form's constructor, AFTER the InitializeComponent call.
Dan C.
@Dan C.: Unfortunately, this approach doesn't let him put it in the component itself, which is (I believe) what he's desiring.
Reed Copsey
+1  A: 

The only thing I know of that would do this, directly from within the control, woudl be to override OnVisibleChanged, and handle your action the first time the control is shown.

However, I would rethink the need for this. This is going to cause your control to behave differently from the standard Windows Forms Controls. Trying to hook your behavior into an event that is non-standard seems like a maintainance and usability nightmare to me...

Reed Copsey
It is intended for this: http://www.codeproject.com/KB/menus/fileselect.aspxRoughly, adding an option "automatically create new document"
peterchen
What are you trying to add after the form loads? Can it be created by default, on initialization instead, and then customized at design time? That seems like a more understandable approach.
Reed Copsey
I see your concern, I've updated the question, maybe that makes it clearer.
peterchen
@peterchen: OnVisibleChanged should work for you, then. It happens after all of the initialization, and after form_load. Everything should be setup by then.
Reed Copsey
I have a follow up question (http://stackoverflow.com/questions/811036/parent-owner-of-a-winforms-component) - how do I get to the parent of a component?
peterchen