i was wondering. How does the applications such as Microsoft Word, Excel etc, create a new form with blank inside it, it is like we create another new form.
I tried the code:
this.Hide();
Form2 secondaryForm = new Form2();
secondaryForm.ShowDialog();
this.Close();
The above code is to create a new blank form (new form), but that is limited time only. What i was wondering is, how does the applications can make a thousand new blank form (new form) in such a unlimited time?
Note: what i mean by Unlimited time is: we can create a new form, the form will always be created, no matter how many times we are click the "new" button
Thanks in advance!
the answer above me are good, but i want to expand them.
the reason that your code isn't working is because you are closing the main form
main form is the form you run when the program start. if you create a simple winform and look at the
main
function you'll see something likeand when you close the Form1 you are coming back to here and continue to run, finish the main function and end the program.
what you can do, on top of what everyone else suggested, is creating a main form that not visible, and create another form right after the creation of the main form. in this way you'll be able to close and open forms as you'd like and that won't close the program.
as for what you've asked, to be able to open several forms. that you can do simply be doing:
the
ShowDialog
method does whatshow
does, but also freeze the form that opened it. so you don't want it probablyI guess you want to
produce
forms once we click on the button, the forms should be produced and shown regardless of the limit to the number of forms. You can use aTimer
for this purpose:You should limit the number of forms because we can't produce as many forms as possible, I've tested and there was an
StackOverflowException
thrown inSystem.dll
with 272 forms opened :))). Try adding acount
variable and stop the timer when it reaches a number for example a 272)It is simple, they just create a new instance of the desired form class and call its
Show
method:You're currently using the
ShowDialog
method, which creates a modal dialog—i.e. one that blocks user interaction with the rest of your application until the user dismisses it. That is not what you want.You also don't need to call the
Hide
andClose
methods unless you actually want to close the original form. The Office applications do not do this—"New" simply opens a new window, leaving the current one intact.If you do want to close the original form when opening a new one, do it like this:
Following code will allow you to open any number of forms without closing the main form.