How can I get the code to run after the form is displayed? (The problem is with "Shown.")

I thought Shown would be the answer. But it seems that not all controls are equal. The panel is displayed immediately, but the shortcut is not.

I have the following code:

 public partial class Form2 : Form { Panel p = new Panel() { BackColor = Color.Green }; Label l = new Label() { Text = "abc", Location = new Point(0, 100) }; public Form2() { Controls.Add(p); Controls.Add(l); Shown += new EventHandler(Form2_Shown); } void Form2_Shown(object sender, EventArgs e) { System.Threading.Thread.Sleep(2000); } } 

What initially shows this:

enter image description here

And after 2 seconds it is:

enter image description here

So, how do I run the code after the "second image"?

+1
source share
2 answers

Add the following to the beginning of the event handler:

 this.Update(); 
0
source
 void Form2_Shown(object sender, EventArgs e) { Application.DoEvents(); System.Threading.Thread.Sleep(2000); } 

I think you get what you want, but IMHO, if you have a long operation, you should start another thread.

+2
source

Source: https://habr.com/ru/post/1384792/


All Articles