views:

107

answers:

4

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!

+5  A: 

I would use Form1_Shown()

Danny T.
Fantastic. I didn't even realize that the Form_Shown event existed. I can tell it's gonna be a good day!
Jordan S
I don't believe he's suggesting the event. I think he's pointing to overriding the OnShown() method?
Ian
Events or overriding. Events, if you are not changing behavior. Overriding, if you are. In context of the question, I would override.
AMissico
+2  A: 
  • 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.

AMissico
-1? Hey, no fair. I was in middle of edit. Can't a guy "post" in order to "pseudo-save"?
AMissico
Given that the OP explicitly said he wants something to happen right after the form is shown on the screen, you can't *get* more appropriate than form.Shown(). A Timer is very clunky for this: if the timing isn't perfect (and it might change on different machines) then the message might show in the middle of another user action. And if you're going to use events anyway, why use Show instead of Shown?
Klay
I like to take control. Shown is after. Show is before. Show gives me the ability to show a different form, message, prompt, or whatever. Shown is too late. Load is too early.
AMissico
"A Timer is very clunky for this". That is why I have three options. The way we use to do it (that still works), the current WinForms way, and a more flexible way. This give the reader ideas on different possible solutions. Instead of a three word answer...duh...use shown event...duh...vote for me ;O)
AMissico
"if the timing isn't perfect (and it might change on different machines)...message might show in...middle of another user action." Not a good argument because 1) if this technique didn't work we wouldn't be using it. 2) "happen right after" can be from 0 to 100ms? Unless the delay is a few seconds, I cannot see many users performing another operation after they just "opened" a window. 3) 100ms is 100ms on any machine. If not then computers would be unreliable. Using Windows.Forms.Timer is not percise but it is good enough in this "right after" case.
AMissico
+2  A: 

Try the "Shown" event:

Form.Show Event

0bj3ct.m3th0d
+5  A: 

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
}
Ian
That's really convenient! Thanks for sharing