Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to call a method right AFTER a form loads?

I have a C# windows forms application. The way I currently have it set up, when Form1_Load() runs it checks for recovered unsaved data and if it finds some it prompts the user if they want to open that data. When the program runs it works alright but the message box is shown right away and the main program form (Form1) does not show until after the user clicks yes or no. I would like the Form1 to pop up first and then the message box prompt.

Now to get around this problem before I have created a timer in my Form, started the timer in the Form1_Load() method, and then performed the check and user prompt in the first Timer Tick Event. This technique solves the problem but is seems like there might be a better way.

Do you guys have any better ideas?

Edit: I think I have also used a background worker to do something similar. It just seems kinda goofy to go through all the trouble of invoking the method to back to the form thread and all that crap just to have it delayed a couple milliseconds!

like image 855
PICyourBrain Avatar asked Jun 02 '10 13:06

PICyourBrain


People also ask

Which function is called for loading of a window form?

Load Event (System. Windows.

Why the form1_load () event is used?

It "Occurs before a form is displayed for the first time." and "You can use this event to perform tasks such as allocating resources used by the form." You can consider it as "initialization".

How do you call a load event in C#?

You have to call Form_load. Form_Load(this, null);

What is form load event?

The Form Load Event in VB . NET. An important event you'll want to write code for is the Form Load event. You might want to, for example, set the Enabled property of a control to False when a form loads. Or maybe blank out an item on your menu.


3 Answers

I would use Form1_Shown()

like image 57
Danny T. Avatar answered Oct 12 '22 23:10

Danny T.


Use the Shown event. It seems to suit what you need, and will only display the first time the form is shown.

Form f1 = new Form();
f1.Shown += new EventHandler(f1_Shown);

public void f1_Shown(object sender, EventArgs e)
{
   // Show dialog in here
}
like image 25
Ian Avatar answered Oct 12 '22 23:10

Ian


Try the "Shown" event:

Form.Show Event

like image 26
0bj3ct.m3th0d Avatar answered Oct 12 '22 23:10

0bj3ct.m3th0d