在C#.NET中重新打开表单时出现问题 [英] Problem reopening a form in C#.NET

查看:61
本文介绍了在C#.NET中重新打开表单时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......
这是我的问题

Hi everyone....
This is my problem

 public partial class Expedientes : UserControl
    {
        FormX AddNewForm = new FormX();

   private void button1_Click(object sender, EventArgs e)
   {
    AddNewForm.button1.Click+= new EventHandler(XEvent);
    AddNewForm.Show(); 
   }

  private void button2_Click(object sender, EventArgs e)
   {
     AddNewForm.Close();
   }

  private void XEvent(object sender, EventArgs e)
   {
    this.textBox1.Text=AddNewForm.textBox1.Text;
   }

}



当我关闭表单并尝试重新打开它时,会发生此错误:
无法访问已处置的对象.

我该怎么解决?

谢谢人们



When I close the form and try to reopen it, this error occurs:
Can not access a disposed object.

How I solve can it?

Thanks people

推荐答案

而不是使用form close使用Form.Hide(); .否则,每次在按钮单击事件中创建一个新实例.表单关闭后即被调用.它会自动处理
Hi instead of using form close use Form.Hide(); . Other wise each time create a new instance in the button click event. Once the form close called. it get dispose automatically


我认为您需要查看隐藏后为何复制此数据,然后再次显示该表单.
尝试将逻辑从当前位置移到其他位置.
I think you need to look at why this data is copied when you hide and then show the form again.
Try moving that logic out of its current position into some other place.


尝试一下,

Try this,

public partial class Expedientes : UserControl
    {
    FormX AddNewForm = null;
   private void button1_Click(object sender, EventArgs e)
   {
    AddNewForm = new FormX();
    AddNewForm.button1.Click+= new EventHandler(XEvent);
    AddNewForm.Show();
   }
  private void button2_Click(object sender, EventArgs e)
   {
     if (AddNewForm!=null)
         AddNewForm.Close();
   }
  private void XEvent(object sender, EventArgs e)
   {
    this.textBox1.Text=AddNewForm.textBox1.Text;
   }
}



如果有帮助,请将其标记为答案.



Mark it as answer if it is helpful.


这篇关于在C#.NET中重新打开表单时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆