无法访问已释放的对象!!!!!

boloverr 2008-10-16 08:50:02
以下是form1的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _20080517S2
{
public partial class MainForm : Form
{
From2 fa;
public MainForm()
{
InitializeComponent();
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);

}

void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(fa.Timu);
fa.Close();


}

private void 增加题目ToolStripMenuItem_Click(object sender, EventArgs e)
{
fa.Show();
}
}
}


下面是form2的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _20080517S2
{
public partial class From2: Form
{
public From2()
{
InitializeComponent();
}
public string Timu
{
get { return this.textBox1.Text; }
}
public string XX1
{
get { return this.textBox2.Text; }
}
public string XX2 {
get { return this.textBox3.Text; }
}
public string XX3 {
get { return this.textBox4.Text; }
}
public string XX4 {
get { return this.textBox5.Text; }
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.Timu);

}
}
}


第一次点击”增加题目“时 form2 正常SHOW出来!

输入完后 点FORM2的Button1关闭

然后第二次点击“增加题目”的时候就会出现 “无法访问已释放的对象” 的错误

为什么会这样

环境:VS2008
...全文
910 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwgboy 2009-03-02
  • 打赏
  • 举报
回复
也算是一种办法!
http://lwgboy.space.mofun.cc
stcsk1226 2008-10-20
  • 打赏
  • 举报
回复
修改这个:

void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(fa.Timu);
fa.Hide();


}

或者

private void 增加题目ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(fa==null)
{
fa = new From2();
}
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
}
fb13579 2008-10-20
  • 打赏
  • 举报
回复
没错。对象已经释放了。
csr_hema 2008-10-20
  • 打赏
  • 举报
回复
窗体关闭时是不是对象默认释放了啊?
vrhero 2008-10-16
  • 打赏
  • 举报
回复
fa.Close();

改为...

fa.Hide();
changjiangzhibin 2008-10-16
  • 打赏
  • 举报
回复
调试 可看到 fa.IsDispose为 true;

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧