简体   繁体   English

处置后如何创建对象?

[英]How do I create an object after it has been disposed?

I dispose a object in my code and I want to now create it again. 我在代码中放置了一个对象,现在我想再次创建它。

How can I do this? 我怎样才能做到这一点?

Answer is : 答案是:

private void showToolStripMenuItem_Click(object sender, EventArgs e)
    {
        xpPanelGroup1.CreateControl();
        xpPanelGroup1.Visible = true;
         ...


    }
    private void noShowToolStripMenuItem_Click(object sender, EventArgs e)
            {
                xpPanelGroup1.Visible = false;
                  ...
                xpPanelGroup1.Dispose();
            }

You need to create a new object after you have called Dispose() . 调用Dispose()之后,需要创建一个新对象。

But if you want to reuse the object later you should not dispose it, you may try to use Hide or .Visible = false or similar if you temporarily want to hide a control. 但是,如果您以后想重用该对象,则不要处置它,如果您要暂时隐藏控件,则可以尝试使用Hide.Visible = false或类似方法。

Edit: In your code you create a new xpPanelGroup1: 编辑:在您的代码中创建一个新的xpPanelGroup1:

UIComponents.XPPanelGroup xpPanelGroup1 = new UIComponents.XPPanelGroup() ;

but that is only local to the showToolStripMenuItem_Click method. 但这仅是showToolStripMenuItem_Click方法的本地方法。 If you just type 如果您只输入

xpPanelGroup1 = new UIComponents.XPPanelGroup() ;

you are using the class member, that is the same variable you dispose in the noShow method. 您正在使用类成员,即与您在noShow方法中配置的变量相同的成员。
But I still recommend just hiding instead of disposing. 但是我仍然建议只隐藏而不是处置。

You should have a look in your design code (the ".designer.cs" file automatically generated by at design time), and try to call it. 您应该查看设计代码(在设计时自动生成的“ .designer.cs”文件),然后尝试调用它。 This is the code run when the widget is instantiated. 这是在实例化窗口小部件时运行的代码。

也许您可以将对象设置为null而不是将其处置,然后在想要再次使用它时将其重新分配给另一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM