0

I have 2 forms and I need to (show/hide) them from main().. I'm trying something like this:

Form1 f1 = new Form1();
Form2 f2 = new Form2();

f1.ShowDialog();
Thread.Sleep(5000);
f1.Close(); // or Hide() both not working
f2.ShowDialog();

But it only shows f1 and not close it or shows f2

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Feras Senjab
  • 211
  • 3
  • 11
  • 3
    The `Thread.Sleep` is not needed here, you are calling `f1` as a dialog, which means that execution stops in that function until `f1` is closed. I'm guessing that if you close `f1`, `f2` will show. If you want to show one form, and while its open show the next one, use `f1.Show()` and `f2.Show()` instead of `ShowDialog`. `ShowDialog` is a blocking call, meaning it will wait for the method to return before continuing to execute the lines beneath it, and it won't return until the dialog is closed (usually with a `DialogResult`). – Ron Beyer Oct 05 '15 at 01:58
  • does form 1 or 2 need to be `Modal` when you are calling the f1.Show..? – MethodMan Oct 05 '15 at 02:02
  • @RonBeyer thanks for answer.. I've tried Show() before asking (without sleep)and when compiling it's not shown.. but from your comment I knew that Show will close when the execution ends.. that's why I didn't see the form. – Feras Senjab Oct 05 '15 at 02:13
  • Can you explain what is supposed to happen in your app? Should the forms display at the same time?...one after the other?...how/when you do need to "show and hide" them? – Idle_Mind Oct 05 '15 at 03:08
  • @Idle_Mind I'm trying to show "please wait" message to the user during achieving some tasks.. then it should hide the "please wait" and shows "finish".. I've some new problems now that I'm searching for answers (form is shown without it's content during execution) – Feras Senjab Oct 05 '15 at 03:44
  • Without more code it's hard to recommend a good approach. Are the task occurring from within the Forms?...or within main() itself? – Idle_Mind Oct 05 '15 at 04:02
  • Do you want to create a [splash screen](http://stackoverflow.com/a/32421479/3110834)? – Reza Aghaei Oct 05 '15 at 07:41

1 Answers1

0

f1.ShowDialog() shows the screen until the form is quit. After that it closes the form f1 which was already closed.

If your are creating a splash screen, there are better ways to do so.

A quick fix in your code would be changing f1.ShowDialog() to f1.Show() which doesn't block your code.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325