无法访问已释放的对象? [英] Cannot access a disposed object?

查看:191
本文介绍了无法访问已释放的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个倒计时表 - 第一个表单上的用户会进入倒计时 - 预警时间,结束信息等,另外还有两个单选按钮(最大值/最小值),并根据所选择,他们将打开一个新的最大或最小形式,其中的时候才会真正开始倒计时。这是工作的罚款,并倒计时如我所料。但是,如果我退出最大值或最小值的形式,并尝试以新的时代我得到的错误再次运行。在code低于 - 注意注释掉.ShowDialog(本);是我尝试过 - 它让我关闭和打开新的形式确定,但它并没有真正开始倒计时。 UpdateLabels是做标签的更新的功能。

I have a countdown Timer form - on the first form the user will enter the countdown time - warning times, end message, etc. There are also two Radio buttons (Max/Min) and depending on which is selected they will open a new Max or Min form where the time will actually start to countdown. It is working fine and counting down as i expect. However if I exit the Max or Min form and try to run again with new times I get the error. The code is below - note the commented out .ShowDialog(this); was something i tried - it let me close and open the new forms ok but it did not actually start the countdown. UpdateLabels is the function which does the updating of Labels.

                bool Max = rbMax.Checked;
                if (Max == true)
                {
                    //_Max.ShowDialog(this);
                    _Max.Show();

                }
                else
                    //_Min.ShowDialog(this);
                    _Min.Show();

                UpdateLabels();
            }

我也试过,我在网上看了一个可能的解决方案下,但它也没有工作......

I also tried the following which i read online as a possible solution but it also did not work....

    private void Max_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.Hide();
        this.Parent = null;
    }

谁能帮我 - 如果需要,我可以张贴UpdateLabels功能。我是pretty的新用户界面C#开发,因此任何帮助将是巨大的。谢谢你。

Can anyone help me out - I can post the UpdateLabels function if needed. I am pretty new to UI C# development so any help would be great. Thanks.

推荐答案

的问题是,一个封闭的形式,不能再使用(重开)。这就是为什么c您的$ C $发布试图阻止关闭和隐藏的仅仅是你的窗口。但这样做,则取消 - 属性必须设置为真:

The problem is, that a closed form can not be used anymore (be reopened). Thats why the code you posted tries to stop closing and only hides your window. But for doing this, the Cancel-property must be set to true:

private void Max_FormClosing(object sender, FormClosingEventArgs e)    {        
   this.Hide();        
   this.Parent = null;    
   e.Cancel=true;
}

要关闭它这种方式后,显示的形式,用Show()方法表现出来。

To show the form after closing it this way, show it with the Show() method.

不过这可能只是一种变通方法,你可以用另一种设计解决问题。 也许这将是明智的,创造的,而不是试图每次重新打开您的形式,每次你需要它的时候,一个新的实例。这也有一个表格只有requesires资源,如果它真正需要的优势。

However this is probably only a workaround and you could solve the problem with another design. Maybe it would be wise, to create a new instance of your form, every time you need it, instead of trying to reopen it every time. This also has the advantage that the form only requesires resources if it is really needed.

这篇关于无法访问已释放的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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