每次单击按钮时创建一个新表单 [英] Create a new form every time a button has been clicked

查看:45
本文介绍了每次单击按钮时创建一个新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想.Microsoft Word、Excel 等应用程序如何创建一个里面有空白的新表单,就像我们创建另一个新表单一样.

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.

我试过代码:

this.Hide();

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

this.Close();

上面的代码是创建一个新的空白表单(new form),不过那只是时间有限.我想知道的是,应用程序如何在如此无限的时间内制作一千个新的空白表格(新表格)?

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

提前致谢!

推荐答案

我上面的答案很好,但我想扩展它们.

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

主窗体是程序启动时运行的窗体.如果你创建一个简单的 winform 并查看 main 函数,你会看到类似

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());

当你关闭 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

ShowDialog 方法和 show 一样,但也会冻结打开它的表单.所以你可能不想要它

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

这篇关于每次单击按钮时创建一个新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆