Create a new form every time a button has been cli

2019-09-22 11:19发布

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!

标签: c# winforms
4条回答
再贱就再见
2楼-- · 2019-09-22 11:29

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 like

     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Form1());

and 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:

Form frm = new Form();// Creating a form
frm.Show();// displaying the form

the ShowDialog method does what show does, but also freeze the form that opened it. so you don't want it probably

查看更多
Bombasti
3楼-- · 2019-09-22 11:36

I 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 a Timer for this purpose:

//Place this code in your main form constructor
Timer t = new Timer {Interval = 100};    
t.Tick += (s,e) => {
   new Form2().ShowDialog();
};
button1.Click += (s,e) => {
   t.Start();
};
//NOTE: you have to use the stop button of the IDE to stop this,
//it will run continuously producing new forms and showing them 
//without allowing you to touch the Close button of your main form.

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 in System.dll with 272 forms opened :))). Try adding a count variable and stop the timer when it reaches a number for example a 272)

查看更多
甜甜的少女心
4楼-- · 2019-09-22 11:46

It is simple, they just create a new instance of the desired form class and call its Show method:

MyForm frm = new MyForm();
frm.Show();

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 and Close 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:

// Create and display new form
MyForm frm = new MyForm();
frm.Show();

// Close this form
this.Close();
查看更多
Rolldiameter
5楼-- · 2019-09-22 11:54

Following code will allow you to open any number of forms without closing the main form.

//this.Hide();

Form2 secondaryForm = new Form2(); 
secondaryForm.Show();

//this.Close();
查看更多
登录 后发表回答