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

4

9

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!

Anurous answered 2/6, 2010 at 13:13 Comment(3)
tags are inappropriate. you should pick "c#" "winforms". tags should mean category of question.Plymouth
Yeah i wasn't sure what to use. I fixed it now. Thanks.Anurous
For future reference, this link contains a list of the order of events when showing a Form: msdn.microsoft.com/en-us/library/86faxx0d%28VS.80%29.aspxHate
H
17

I would use Form1_Shown()

Heartburning answered 2/6, 2010 at 13:18 Comment(3)
Fantastic. I didn't even realize that the Form_Shown event existed. I can tell it's gonna be a good day!Anurous
I don't believe he's suggesting the event. I think he's pointing to overriding the OnShown() method?Moidore
Events or overriding. Events, if you are not changing behavior. Overriding, if you are. In context of the question, I would override.Devotional
M
7

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
}
Moidore answered 2/6, 2010 at 13:25 Comment(1)
I'm trying to use your solution seems like the GUI is up but it doesn't render the controller until it finishes tasks on the Shown event. Any advice for me?Handkerchief
S
4

Try the "Shown" event:

Form.Show Event

Steamheated answered 2/6, 2010 at 13:21 Comment(0)
D
2
  • 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.

Devotional answered 2/6, 2010 at 13:19 Comment(5)
-1? Hey, no fair. I was in middle of edit. Can't a guy "post" in order to "pseudo-save"?Devotional
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?Depicture
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.Devotional
"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)Devotional
"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.Devotional

© 2022 - 2024 — McMap. All rights reserved.