Reputation: 989
I have (what should be) a very simple Windows Forms application. There is a "Main" form that is a bunch of data input with a Submit button. When the user presses Submit, I want to display a second form that is a very basic form with only a label on it saying something like, "Please wait...". I created the second form, and in the Submit button's Click() I do this:
Form2 f2 = new Form2();
f2.Show();
System.Threading.Thread.Sleep(3000);
f2.Hide();
Form2's ctor:
public Form2()
{
InitializeComponent();
this.Text = "Form2 that is useless";
}
What I see is the Form2, but where the label should be, I only see the outline of it as a white reactangle with no text. Text foreground and background colors check out (black foreground on "Control" colored background.
I am pulling my hair out here trying to determine why a simple form added with no property changes will not display controls properly. I added a button control and a picture box control and got the same results. the control's outline shows up, but the contents do not.
Help?
Upvotes: 0
Views: 2665
Reputation: 27633
Add:
Shown += new EventHandler(Form2_Shown);
to Form2's ctor. And:
void Form2_Shown(object sender, EventArgs e)
{
Update();
}
In its class.
EDIT:
This doesn't work. Probably because the Sleep
is executed before the Shown
event handler. The solution is to add a:
f2.Update();
before the Sleep
, instead of the previous solution.
This time I tried it. It works.
Upvotes: 3
Reputation: 81610
You aren't giving Form2 a chance to render itself.
Try using a Timer on your second form (instead of that Sleep()
method you have) that will close it after your 3,000 milliseconds. Start it in the Shown event. Close the form in the Tick event.
public Form2() {
InitializeComponent();
this.Text = "Form2 that is useless";
timer1.Interval = 3000;
}
private void Form2_Shown(object sender, EventArgs e) {
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e) {
this.Close();
}
Upvotes: 2