我想在子窗体关闭时在MdiParent窗体中更改标签的文本。但是我收到此错误“ yourprogram.exe中发生了'System.NullReferenceException'类型的未处理异常”。这是我的代码:
private void Employees_FormClosing(object sender, FormClosingEventArgs e)
{
(MdiParent as MainForm).setStatusText = "Ready";
}
我的MdiParent表单中有以下代码:
public string setStatusText
{
set
{
tsStatus.Text = value;
}
}
我也对Employees_FormClosed事件进行了尝试,但仍然收到相同的错误。我只是不明白为什么在实例化类时为什么给我一个空引用。
此代码来自您的评论:
private void addEmployeeToolStripMenuItem_Click(object sender, EventArgs e)
{
Employees emp = new Employees();
emp.MdiParent = this.MdiParent;
emp.Show();
tsStatus.Text = "Adding Employee";
}
当我理解正确时,您的setStatusText
方法与该addEmployeeToolStripMenuItem_Click
方法在同一类中。这意味着该行emp.MdiParent = this.MdiParent;
是错误的。应该是emp.MdiParent = this;
,因为您不想将父母设为孩子,所以您希望将自己设为孩子的父母。
编辑:
当您获得一个NullReferenceException
使用时as
可能意味着两件事。在这种情况下MdiParent
,null
您的变量是,或者在这种情况下,变量的类型不正确MainForm
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句