How to Execute Code After a Form Has Loaded

How do I execute code AFTER a form has loaded?

You could use the "Shown" event: MSDN - Form.Shown

"The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event."

How can I execute code after my form starts?

Processing that occurs after Application.Run is usually triggered in the form's Load event handler. You can easily create a Load method in Visual Studio by double clicking any open space on the form.

This would look something like this.

    private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
{
if (CallSomeMethod())
{
this.SetLogText("True");
}
}

The reason this is (as stated in several other answers) is that the main thread (the one that called Application.Run(window)) is now taken up with operating the Message Pump for the form. You can continue running things on that thread through messaging, using the form's or forms' events. Or you can start a new thread. This could be done in the main method, before you call Application.Run(window), but most people would do this in Form_Load or the form constructor, to ensure the form is set up, etc. Once Application.Run returns, all forms are now closed.

How to execute code every time a form is shown?

There is an event called Shown that gets fired after Show is called.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.shown?view=netframework-4.8

There is another that is called when it is Activated
I think this is what you want

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.activated?view=netframework-4.8

There is also VisibleChanged on controls.

It is all documented here:

https://learn.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms?view=netframework-4.8

This includes focus and validation events for forms at the bottom of the page.

Execute code when form loaded and before Form closes

I suggest you read through the WinForms event ordering as posted on MSDN:

http://msdn.microsoft.com/en-us/library/86faxx0d.aspx

Wait until form is finished loading

try to use the shown event something like this

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Shown += new System.EventHandler(this.Form1_Shown);
}

private void Form1_Shown(object sender, EventArgs e)
{

}
}

hope this help

How to run code when form is shown?

There is a Shown event for a windows form. Check out: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx

Or if you are lazy, here you go:

public Form1()
{
InitializeComponent();
Shown += Form1_Shown;
}

private void Form1_Shown(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Text = "Done";
}

Enjoy.



Related Topics



Leave a reply



Submit