winform 无法访问已释放的对象! 20
以下是form1的代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;us...
以下是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 展开
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 展开
1个回答
展开全部
你好,
其实很简单,
首先在这里:
From2 fa;
public MainForm()
{
InitializeComponent();
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
你在MainForm构造函数里调用了fa的初始化代码fa = new From2();
然后,
“输入完后 点FORM2的Button1关闭 ”
到这里,Form2被关闭,也就是fa所指向的窗体被关闭了
fa被释放掉了……
所以你第二次点“增加题目”的时候,fa只是一个空引用了……
试试这样写:
把MainForm构造函数里的
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
写到增加题目里,也就是:
private void 增加题目ToolStripMenuItem_Click(object sender, EventArgs e)
{
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
fa.Show();
}
Button1里增加判断:
void button1_Click(object sender, EventArgs e)
{
if(fa!=null)
{
MessageBox.Show(fa.Timu);
fa.Close();
}
}
其实很简单,
首先在这里:
From2 fa;
public MainForm()
{
InitializeComponent();
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
你在MainForm构造函数里调用了fa的初始化代码fa = new From2();
然后,
“输入完后 点FORM2的Button1关闭 ”
到这里,Form2被关闭,也就是fa所指向的窗体被关闭了
fa被释放掉了……
所以你第二次点“增加题目”的时候,fa只是一个空引用了……
试试这样写:
把MainForm构造函数里的
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
写到增加题目里,也就是:
private void 增加题目ToolStripMenuItem_Click(object sender, EventArgs e)
{
fa = new From2();
fa.MdiParent = this;
fa.WindowState = FormWindowState.Maximized;
fa.button1.Click += new EventHandler(button1_Click);
fa.Show();
}
Button1里增加判断:
void button1_Click(object sender, EventArgs e)
{
if(fa!=null)
{
MessageBox.Show(fa.Timu);
fa.Close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询