views:

461

answers:

1

When is the proper time to restore the UI layout settings of a System.Windows.Forms.Control?

I tried this:

FooBarGadget control = new FooBarGadget();
parent.Controls.Add(control);
control.FobnicatorWidth = lastLayoutSettings.FobWidth;

No cigar. Reason? control isn't finished laying out its internals; it's in its default size of 100x100 pixels after construction. Once it's done loading and actually displays in the UI, it will be 500x500 pixels. Therefore, setting the FobnicatorWidth to 200 pixels fails; it's larger than the control.

Is there a control.Loaded event - somwehere where I can restore my saved UI settings?

+2  A: 

If you're creating this control as part of loading a new Form, a good place to reload saved settings would be in Form.OnLoad (or respond to the the Form.Load event). Another event that might be helpful is Control.HandleCreated, which happens when the underlying window of your control is created.

If neither of these helps, perhaps more information about your particular scenario will help us get to a better answer.

Charlie
It's not part of a form, otherwise I'd use Form.OnLoad. I will try HandleCreated event.
Judah Himango
Hi Charlie. Just wanted to let you know I tested the HandleCreated event and this does indeed appear to work. Thanks!
Judah Himango
Charlie, after a few months of using this, we've discovered that HandleCreated doesn't always work. For nested controls, sometimes it's doesn't work. Using the .Layout event instead seems to work.
Judah Himango
Very interesting. I'd be interested to know why this is, if you figure it out at some point.
Charlie
Charlie, yet more info...the Layout event doesn't always work either. Grrrrrr....will get back to you when I find out more info.
Judah Himango
What I've discovered is that nested controls cannot be restored on HandleCreated. The proper way to do this is to wait for nested controls to fire their Layout event. When that happens, if their handle is created, you can restore settings on these controls.
Judah Himango