Notification when my form is fully loaded in C# (.Net Compact Framework)?
I have a form in my application and I want to do some processing when my form has been
Fully loaded but I have no event or something which I can bind to when load is finished.
Does anyone has any idea, how can I do this?
What exaclty mean "fully loaded" ? Do you mean, the "Load" event was successfully proceeded?
You can do this:
public class MyForm : Form {
protected override void OnLoad( EventArgs e ) {
// the base method raises the load method
base.OnLoad( e );
// now are all events hooked to "Load" method proceeded => the form is loaded
this.OnLoadComplete( e );
}
// your "special" method to handle "load is complete" event
protected virtual void OnLoadComplete ( e ) { ... }
}
But if you mean "fully loaded" the "form is loaded AND shown" you need override the "OnPaint" method too.
public class MyForm : Form {
private bool isLoaded;
protected override void OnLoad( EventArgs e ) {
// the base method raises the load method
base.OnLoad( e );
// notify the "Load" method is complete
this.isLoaded = true;
}
protected override void OnPaint( PaintEventArgs e ) {
// the base method process the painting
base.OnPaint( e );
// this method can be theoretically called before the "Load" event is proceeded
// , therefore is required to check if "isLoaded == true"
if ( this.isLoaded ) {
// now are all events hooked to "Load" method proceeded => the form is loaded
this.OnLoadComplete( e );
}
}
// your "special" method to handle "load is complete" event
protected virtual void OnLoadComplete ( e ) { ... }
}
I think the OnLoad
event isn't really what you want, as it occurs before the form is displayed. You can use Application.Idle
with OnLoad
to make an OnLoaded
event though:
protected override void OnLoad(EventArgs args)
{
Application.Idle += new EventHandler(OnLoaded);
}
public void OnLoaded(object sender, EventArgs args)
{
Application.Idle -= new EventHandler(OnLoaded);
// rest of your code
}
you should be able to use the OnLoad event.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onload.aspx
My approach is similar to the accepted answer from TcKs. The problem I faced was that I had an event handler for a group of controls that responded to VisibleChanged with the event handler moving controls about within a Panel. Trouble was (of course) that the Visibility changes when the form is first loaded - but after the .Load() event.
I created and set a bool value for the Form:
private bool formShown = false;
and then added the following line to the Form_Load()
this.Paint += (s, args) => { formShown = true; };
with the first line of my VisibleChanged() event handler as:
if(!formShown) { return; }
Succinct and functional.
You can use the Load
event of the form. In the constructor of your Form write following line
this.Load += new System.EventHandler(this.Form1_Load);
and then write the following method in which you can do some stuff.
void Form1_Load(object sender, EventArgs e)
{
// do some stuff here.
}
精彩评论