我有以下代码(这是我试图在其中包含所有重要信息的摘录)
public partial class MyTopScreen : Form
{
public bool _myVariable ;
public CommonForm _commonGridForm;
public MyTopScreen(bool first_param, string second_param )
{
CommonForm commonGridForm = new CommonForm();
DataGridView dataGridView1 = commonGridForm.dataGridView1;
_commonAttributForm = commonAttributForm;
InitializeComponent();
this.TabPage1.Controls.Add(dataGridView1);
}
}
然后当我在CommonForm
内时,如下所示:
public partial class CommonForm : Form
{
public CommonForm()
{
//My Question is how do I access the _myVariable from this point in the code ?
//this.parent doesnot work or does not give me access to _myVaribale
}
}
提前感谢您的帮助。
答案 0 :(得分:3)
您必须将父级转换为适当的类型:
MyTopScreen myParent = this.parent as MyTopScreen;
if (myParent != null)
{
bool myVariableFromParent = myParent._myVariable;
}
只有小心转换,因为如果父级是不同的控件,那么在尝试访问_myVariable
时,您将获得空引用异常。另外,不要尝试直接强制转换((MyTopScreen)this.parent
),因为如果父级是不同的控件,您将获得强制转换异常。
答案 1 :(得分:3)
一种方法是将MyTopScreen
的实例传递给其他类'构造函数,如果它与它有关。将它存放在一个字段中。
public partial class CommonForm : Form
{
public CommonForm(MyTopScreen screen)
{
this.TopScreen = screen;
}
private MyTopScreen TopScreen{ get; set; }
}
现在,您可以通过CommonForm
TopScreen._myVariable.
中访问该变量了
你必须将它传递给构造函数:
CommonForm commonGridForm = new CommonForm(this);
答案 2 :(得分:3)
this.Parent
不起作用的原因是因为您从未将MyTopScreen
设置为父级。您需要在代码中添加commonGridForm.Parent = this
。然后你可以拨打((MyTopScreen) this.Parent)._myVariable
。但这只是为了让您当前的代码正常工作;它肯定是不是最好的方法。
我建议的一个改进是不公开公开您的字段,而是将它们用作通过属性公开的支持字段(请参阅this)。
此外,您可以通过构造函数将对父窗体的引用传递给子窗体。以下是您的代码的样子:
public partial class MyTopScreen : Form
{
//keep your fields private.
private bool _myVariable;
private CommonForm _commonGridForm;
//expose fields through properties.
public bool MyVariable
{
get { return _myVariable; }
}
public CommonForm CommonGridForm
{
get { return _commonGridForm; }
}
public MyTopScreen(bool first_param, string second_param)
{
CommonForm commonGridForm = new CommonForm(this);
DataGridView dataGridView1 = commonGridForm.dataGridView1;
_commonAttributForm = commonAttributForm;
InitializeComponent();
this.TabPage1.Controls.Add(dataGridView1);
}
}
public partial class CommonForm : Form
{
private MyTopScreen _parent;
//inject parent reference in child form's constructor.
public CommonForm(MyTopScreen withParent)
{
_parent = withParent;
//_parent.MyVariable <-- here's how you'd access your var.
}
}
答案 3 :(得分:2)
创建CommonForm类的参数化构造函数(Constructor Overloading)并将bool值传递给它。
public partial class MyTopScreen : Form
{
public bool _myVariable ;
public CommonForm _commonGridForm;
public MyTopScreen(bool first_param, string second_param )
{
CommonForm commonGridForm = new CommonForm(_myVariable);
DataGridView dataGridView1 = commonGridForm.dataGridView1;
_commonAttributForm = commonAttributForm;
InitializeComponent();
this.TabPage1.Controls.Add(dataGridView1);
}
}
public partial class CommonForm : Form
{
public CommonForm(bool _myVariable)
{
//Here you access the variable
}
}
答案 4 :(得分:1)
有几种方法可以做到这一点。如果值不会改变,您可以将其传递给CommonForm的构造函数(推荐):
public partial class CommonForm : Form
{
public CommonForm(bool myVariable)
{
// available
}
}
CommonForm commonGridForm = new CommonForm(_myVariable);
如果它可以更改,您可以将实例传递给构造函数中的父窗体:
public partial class CommonForm : Form
{
public CommonForm(MyTopScreen parent)
{
// available
parent._myVariable;
}
}
CommonForm commonGridForm = new CommonForm(this);