从第一个表格关闭第二个表格 [英] Close second form from first form

查看:94
本文介绍了从第一个表格关闭第二个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

现在有一个表单(表单A),具有2个按钮,即OPEN和CLOSE.当我单击打开"按钮时,将显示第二个表格(表格B).再一次,当我在Form A上按CLOSE按钮时,我的第二个表单被关闭.到现在为止都没问题.然后再次在窗体A上单击打开"按钮,发生以下错误,
无法访问已处置的对象.对象名称:"Form B".".如何解决此问题...欢迎所有答复.我的编码是

Dear All,

Now am having one form(Form A),having 2 buttons ie, OPEN and CLOSE. When am click button OPEN, my second form(Form B) is display. And again when am press the button CLOSE at Form A, My second form was closed. Till now no problem. And again am click OPEN button at Form A, following error occured,
"Cannot access a disposed object.Object name: ''Form B''.". How to overcome this problem... All replies are welcome. My coding is

FormB  _form2 = new Form2();
        private void btn_open_Click(object sender, EventArgs e)
        {
          
            _form2.2how();

        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            _form2.Close();

        }





在此先感谢

Dhinesh Kumar.V





Thanks in Advance

Dhinesh Kumar.V

推荐答案

您可以使用form2.Hide()代替form2.Close().如果您坚持使用form2.Close(),则当看到form2为null时,必须实例化一个新的form2对象.在关闭按钮的click方法中,您应该在调用Close()方法之后将其设置为form2 = null.

问候,

—曼弗雷德(Manfred)
[修改]
You could use form2.Hide() instead of form2.Close(). If you insist on using form2.Close() you would have to instantiate a new form2 object when you see that form2 is null. In the close button''s click method you should then se form2 = null after having called the Close() method on it.

Regards,

— Manfred
[Modification]
MSDN [ ^ ]写道:

关闭表单时,将关闭在对象内创建的所有资源,并处置该表单.通过处理Closing事件并设置作为参数传递给事件处理程序的CancelEventArgs的Cancel属性,可以防止在运行时关闭窗体.如果您要关闭的表单是应用程序的启动表单,那么您的应用程序将结束.

表单未放置在关闭"上的两个条件是:(1)它是多文档界面(MDI)应用程序的一部分,并且该表单不可见; (2)您已经使用ShowDialog显示了表单.在这种情况下,您将需要手动调用Dispose来标记表单的所有控件以进行垃圾收集.

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form''s controls for garbage collection.


FormB  _form2;

private void btn_open_Click(object sender, EventArgs e)
{

   if(_form2==null || _form2.IsDisposed)
   {
      _form2= new Form2();

   }
   _form2.show();
}


public partial class form1 : Form
{
   
   public void btn_Open_Click(object sender, EventArgs e)
   {
     frm as _form2 = new _frm2()
     frm.show;
   }

   public void btn_Close_Click(object sender, EventArgs e)
   {
       frm as _form2 = new _frm2()
       frm.close
   }
}


这篇关于从第一个表格关闭第二个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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