我有多个表单应用程序。表单1是用户验证的登录表单。Form1转到Form2(菜单表单)。表格2导致Form3仅是弹出窗口,并在打开时隐藏了Form2。Form3转到Form4。现在从Form4开始,使用按钮单击,我需要在不创建新实例的情况下还原Form2。我尝试使用Singelton方法,获取错误。如上所述,编码如下。
form1:
private void click_Click(object sender, EventArgs e)
{
if ((user.Text == username) && (pswd.Text == password))
{
Form2 menu = new Form2();
menu.Username = user.Text;
//hides the form1
this.Hide();
menu.ShowDialog();
}
}
form2:
private static Form2 instance;
public static Form2 Instance
{
get
{
if (instance == null)
{
instance = new Form2();
}
return instance;
}
}
private void button_Click(object sender, EventArgs e)
{
//Hide the form2
Hide();
//Bring up your PopUp form
using (Form3 form3 = new Form3())
form3.ShowDialog();
}
form3:
private void button_Click(object sender, EventArgs e)
{
Hide();
Form4 form4 = new Form4();
form4.Show();
}
form4:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.Instance;//error occured as Mainmenu does not contain a reference for Instance and no extension method accepting a first argument of type 'Mainmenu'
}
基本上,我想恢复被遗忘的form2。
如何跟踪父母的表格,以便您可以直接访问。例如,在您的表单中:
menu.Parent = this;
Hide();
menu.ShowDialog();
然后在form2中以form3称为:
using (Form3 f3 = new Form3())
{
Hide();
f3.Parent = this.Parent;
f3.ShowDialog();
}
然后当您要处置form3:
时this.Parent.Show();
this.Parent.Focus();
this.Dispose();
按照您需要的尽可能多的开放形式的东西。如果您需要返回前面的表单,请使父母" this"而不是" this.parent"