How to Run code After Form_Load

  • Thread starter Thread starter Lynn
  • Start date Start date
L

Lynn

I need to auto paint line at Form_Component(Ex: button,
RichEdit, etc....)
I write code in Form_Load, the code run before the Form
showing, so it can't paint at Form_Component
How to Run code after Form_Load(mean after the Form showed)
 
Lynn,

Put your code in the Form_Activated event. This event gets called after the
form has been loaded. However, this event also gets called every time the
form is re-activated (i.e. changing focus to another form and then back
again) so you may want to use a flag so that your code in that event is only
executed for the first activation.

HTH,
Gary
 
Gary Milton said:
form has been loaded. However, this event also gets called every time the
form is re-activated (i.e. changing focus to another form and then back
again) so you may want to use a flag so that your code in that event is only
executed for the first activation.

Or, a cleaner method, instead of a flag, simply unsuscribe from the
Activated event once you've received it once:

private void MainForm_Activated(object sender, System.EventArgs e)

{

this.Activated -= new System.EventHandler(this.MainForm_Activated);

//do whatever you want here

}
 
Override the base method instead of using an event ...

protected override OnLoad(EventArgs e) {

base.OnLoad(e); // this runs all of the standard Form_Load code ...

this.DoSomethingMore(); // This is your method that needs to be run ...
}

Martin.
 
Back
Top