所以我基本上有一个已打开的表单,我需要检查哪个表单可以打开
“父母表格”
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm();
nf.Show()
}
“已打开的表单”
private void newForm_Load(object sender, EventArgs e)
{
if parent is ("oldForm") // Need to know how to code for this line.
{
//do some stuff here
}
else
{
//Do something different
}
}
例如,如果oldForm是调用此表单的表单,则将发生某些特定的事情,例如,如果“ anotherForm”调用它,则不会发生
答案 0 :(得分:-1)
您可以在表单中添加一个“父母”属性,然后在调用“显示”之前进行设置。
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm();
nf.Parent = this;
nf.Show()
}
您的表单将如下所示:
public class MyForm
{
public Form Parent {get;set;}
private void newForm_Load(object sender, EventArgs e)
{
if (this.Parent is oldForm)
{
//do some stuff here
}
else
{
//Do something different
}
}
}
请注意,if (this.Parent is oldForm)
等同于if (this.Parent.GetType() == typeof(oldForm))
正如其中一条评论所述,如果仅使用Parent属性来做出这一决定,则最好将其定义为布尔型属性,称为DoSomething
,以指示其作用。结合其他建议可以得出:
public class MyForm
{
private bool specialMode;
public MyForm(bool mode)
{
this.specialMode = mode;
}
private void newForm_Load(object sender, EventArgs e)
{
if (this.specialMode)
{
//do some stuff here
}
else
{
//Do something different
}
}
}
您会这样称呼:
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm(true); // SpecialMode = ON
nf.Show()
}