There is no method Close , Finalize is protected, sending WM_CLOSE also gives nothing.

  • First of all, clarify what you mean by "destroy"? Hide it from the form (control.Visible = false) ? Remove it from the form? Free up their resources? - Modus

2 answers 2

You need to remove it from the form:

 Controls.Remove(userControl); 

And in order to completely kill, you need to clear all references to it and call the garbudge collector, but usually this is unnecessary.

 GC.Collect(); 
  • Also, if the control implements the IDisposable interface, you need to call the Dispose method after removing the control from the collection. Note: explicitly calling garbage collection is not very recommended. First of all, it’s best to see, and secondly, the call to GC.Collect() does not guarantee the release of memory from under those objects that are not referenced. - andreycha
  • @andreycha; The Dispose method explicitly frees unmanaged resources that are also managed to be marked for removal by the garbage collector. Also GC.Collect() [guarantees] [1] freeing memory of those objects that do not have references in the managed code. Also, if you read [here] [2], then Dispose does not guarantee the release of the managed memory occupied by the object - it prepares it for reuse. [1]: msdn.microsoft.com/ru-ru/library/xe0c2357 [2]: msdn.microsoft.com/ru-ru/library/… - Chad
  • one
    1) It depends on how it is implemented. It may or may not. 2) In general, does not guarantee. Since if the object has the Finalize() method defined, then after the first collection the object will be moved to the next generation and put in the finalization queue. And only after the finalizer has completed and the assembly reaches this generation, will the memory be released. It is because of the filling of such subtleties that they are not recommended to command the collector explicitly. 3) Yes, I know. But if you want to part with the object, then this method is highly desirable to cause (and the question was about that). - andreycha

UserControl.Dispose()

  • Do not be misleading, as well as read the documentation. Calling Dispose() does not destroy the object and does not free the memory it occupies. The Dispose() method is designed to release the resources that this class uses. - andreycha
  • one
    Is it about freeing the memory? - AlexeyM