我知道如何以模态模式转到另一种形式,就像我在下面所做的那样:
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
}
}
答案 0 :(得分:9)
当您在表单上调用ShowDialog
时,它会一直运行,直到表单关闭,表单的DialogResult
属性设置为None
以外的其他属性,或带有{的子按钮单击{1}}以外的{1}}属性。所以你可以做类似
DialogResult
虽然如果您在单击按钮时从表单返回时没有做任何事情,您可以在表单设计器中的Form2.button1上设置None
属性,并且您不需要事件Form2中的处理程序。
答案 1 :(得分:0)
我在所有多个表单应用中使用静态表单值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
}