Usercontrol Problem


New member
Jun 23, 2011
Programming Experience
I remember my professor using a usercontrol that is connected to a delegate and has an event declared. Sorry I don't know the term.
He is disposing the usercontrol then reuse it like nothings happen.
I try to do the same, but my usercontrol is misplace when i reuse it after disposing.

How can I reuse it without redeclaring the usercontrols properties. Thanks.

I forgot to tell that the usercontrol is inside a Form.
Disposing by implementing IDisposable is a client code cleanup feature, the standard code pattern allows for cleanup of managed and unmanaged resources and sets a Boolean flag to indicate the object has been disposed. This flag can be used in other places in code to prevent usage after the object has been called for disposal. The object lives on after this until all references to it has been released, only after that can the garbage collector collect it. For a simple class you define yourself everything the disposing functionality does can be fully controlled, even revoke the disposed flag and keep using the object, ie making the dispose work more like a 'flush'. For control class that UserControl ultimately inherits the disposing functionality is already mostly defined by the .Net class, hereby removing the control from its parent, and destroying the control and all its child controls, the internal disposed flag can't be revoked here and is enforced several places in the Control class code. So even if you keep the object reference, nothing good will come out of attemting to reuse it after Dispose is called, at this point if you need such a control again you should create a new object instance.

If you need to keep using the object I'd say you shouldn't dispose it. If you need to replicate settings for a new object perhaps you can implement IClonable interface and copy settings, though it will be more difficult for a UserControl that generally also contains multiple child controls.
Top Bottom