返回到上一个表单(c#)
我知道如何在模态模式下转到另一个窗体,就像我在下面所做的那样:返回到上一个表单(c#),c#,forms,modal-dialog,back-button,C#,Forms,Modal Dialog,Back Button,我知道如何在模态模式下转到另一个窗体,就像我在下面所做的那样: public partial class Form1 : Form { public Form1() { InitializeComponent(); } Form2 myNewForm = new Form2(); private void button1_Click(object sender, EventArgs e) { this.Hide()
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 myNewForm = new Form2();
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
myNewForm.ShowDialog();
}
}
这是我的第二张表格,我如何回到上一张表格
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
以外的其他属性,或者单击带有DialogResult
属性的子按钮,而不是None
。所以你可以做一些像
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中的事件处理程序。我在所有多个表单应用程序中使用当前的静态表单值
public static Form1 Current;
public Form1()
{
Current = this;
// ... rest of constructor
}
然后在表格2中
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
}
private void按钮1\u单击(对象发送者,事件参数e)
{
this.Hide();
//我应该在这里放什么来再次展示form1
Form1.Current.ShowDialog()//