Pergunta

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!

Foi útil?

Solução

I would use Form1_Shown()

Outras dicas

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
}

Try the "Shown" event:

Form.Show Event

  • Using a Windows.Forms.Timer is a good, stable, well-known, and easily understood technique for doing what you want. I would avoid any other timer objects.

  • The form's Shown event works well.

  • Overload / override the Show method. (My preferred technique for greater control.) In this method, I would do the checking needed. When ready, I would call the base.Show method, then do any other processing, such as message boxes, prompts, logging, or whatever.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top