日期:2014-05-17 浏览次数:20884 次
窗口1 的代码 namespace HistoryGame { public delegate void CloseForm(); public partial class FrmSuspend : Form { public FrmSuspend() { InitializeComponent(); } public event CloseForm FrmSuspendClose; public void picContinue_Click(object sender, EventArgs e) { FrmSuspendClose(); } 窗体2 namespace HistoryGame { public partial class FrmSuspendCopy : Form { public FrmSuspendCopy() { InitializeComponent(); } FrmSuspend frc = new FrmSuspend(); void CloseBySuspend(bool topmost) { this.DialogResult = DialogResult.OK; this.Close(); } private void FrmSuspendCopy_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { frc.FrmSuspendClose += new CloseForm(CloseBySuspend); } } } 为什么窗体1单击后出现 FrmSuspendClose(); 未进行实例化。的错误,请问什么情况。
namespace HistoryGame { public partial class FrmSuspendCopy : Form { public FrmSuspendCopy() { InitializeComponent(); } FrmSuspend frc = new FrmSuspend(); void CloseBySuspend(bool topmost) { this.DialogResult = DialogResult.OK; this.Close(); } private void FrmSuspendCopy_Load(object sender, EventArgs e) { frc.FrmSuspendClose += new CloseForm(CloseBySuspend); } private void pictureBox1_Click(object sender, EventArgs e) { } } }
------解决方案--------------------
窗体 1中 FrmSuspendClose () 实例在哪? //就是这个frc.FrmSuspendClose += new CloseForm(CloseBySuspend);
void CloseBySuspend(bool topmost)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
这个改成 public 然后在 窗体1中 绑定到 FrmSuspendClose 上