什么是用C#形式之间切换的最佳方法? [英] What's the best method to switch between forms in C#?
问题描述
什么是用C#
情景形式之间切换的最好方法?我想点击一个按钮,在Form1中,杀死现有Form1并创建窗体2。
我怎样才能做到这一点?
Scenario: I want to click a button in a Form1, kill the existing Form1 and create Form2. How can I achieve this ?
我将十分感激,如果有人可以甚至在的WinForms比较这一任务的复杂性VS WPF。
I would much appreciate it, if someone could even compare the complexity of this task in Winforms vs WPF.
推荐答案
要做到这一点,最简单的方法就是有打开两个的形式控制功能。当单击第一个窗体上的按钮,将其关闭并移动到第二个形式。
The easiest way to do this is to have a controlling function that opens both of the forms. When the button is clicked on the first form, close it and move onto the second form.
using (var form1 = new Form1() ) {
form1.ShowDialog();
}
using (var form2 = new Form2()) {
form2.ShowDialog();
}
有关WPF的代码是相似的。最大的区别是,WPF窗口(和几乎所有类作为一项规则)不实现IDisposable,因此不需要使用的语句。
The code for WPF is similar. The biggest difference is that WPF windows (and pretty much all classes as a rule) do not implement IDisposable and hence do not require the using statement.
var win1 = new Window1();
win1.ShowDialog();
var win2 = new Window2();
win2.ShowDialog();
这篇关于什么是用C#形式之间切换的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!