回到上一个表格(c#)
我知道如何在模态模式下转到另一种forms,就像我在下面所做的那样:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } Form2 myNewForm = new Form2(); private void button1_Click(object sender, EventArgs e) { this.Hide(); myNewForm.ShowDialog(); } }
这是我的第二种forms,我该如何回到之前的表格?
public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.Hide(); // what should i put here to show form1 again } }
当您在窗体上调用ShowDialog
时,它将一直运行,直到窗体关闭,窗体的DialogResult
属性设置为None
以外的其他属性,或者单击具有除None
之外的DialogResult
属性的子按钮。 所以你可以做点什么
public partial class Form1 { ... private void button1_Click(object sender, EventArgs e) { this.Hide(); newform.ShowDialog(); // We get here when newform's DialogResult gets set this.Show(); } } public partial class Form2 { ... private void button1_Click(object sender, EventArgs e) { // This hides the form, and causes ShowDialog() to return in your Form1 this.DialogResult = DialogResult.OK; } }
虽然如果您在单击按钮时从窗体返回时没有做任何事情,您可以在窗体设计器中的Form2.button1上设置DialogResult
属性,而根本不需要Form2中的事件处理程序。
我在所有多个表单应用中使用静态表单值Current。
public static Form1 Current; public Form1() { Current = this; // ... rest of constructor }
然后在Form2中
public static Form2 Current; public Form2() { Current = this; // ... rest of constructor }
然后你可以点击你的按钮做,
private void button1_Click(object sender, EventArgs e) { this.Hide(); // what should i put here to show form1 again Form1.Current.ShowDialog(); // <-- this }