Why is the checking account reduced if the form is closed?

I have a panel that displays Form objects. When the Close() method is called on the form, panel.Controls.Count changes, the graph is smaller. How is this possible?

So I show the form in the panel:

 // insertForm is a windows form insertForm.TopLevel = false; insertForm.Dock = DockStyle.Fill; insertForm.FormBorderStyle = FormBorderStyle.None; this.pnlContent.Controls.Clear(); this.pnlContent.Controls.Add(insertForm); 

And when form.Close() is called somewhere, the Controls pnlContent counter is 0. So, how is this possible? What happens during a call to Close() ?

+4
source share
3 answers

Closing the form will delete the form, which in turn will remove it from the parent control collection.

If you use a decompiler, you will notice two things:

  • When you add a control to a ControlCollection , the owner of the control is set as the owner of the ControlCollection's . ControlCollection.Add(Control value) calls value.AssignParent(this.owner) .
  • When a control is removed, it is removed from its ControlCollection owner. Essentially, Control.Dispose calls parent.Controls.Remove(this) .

Lesson learned: Do not use ControlCollection as a general type of collection for controls. It is deeply integrated into WinForms to control containment and parent-child relationships. Use this type only when you really need the parent control to contain a child control.

+5
source

When the form is closed, all resources created inside the object are closed and the form is placed

http://msdn.microsoft.com/pt-br/library/system.windows.forms.form.close.aspx

+1
source

The Controls collection performs two important tasks. First, it tracks the child controls of the container. The order of the management objects in the collection determines their Z-order. Secondly, he ensures that controls are removed when their parent is destroyed. For this reason, you do not need to manage the controls yourself, as you often have to do with other .NET classes that implement IDisposable.

Which leads to an unpleasant error in your code, the Controls.Clear () method simply removes the controls from the collection, but does not delete them. This is a very bad leak that will make your program bold and slow, which will ultimately crash when Windows refuses to provide it with more window handles. You will need to write this as follows:

 while (this.pnlContent.Controls.Count > 0) this.pnlContent.Controls[0].Dispose(); 

Which now also makes it obvious why the control counter decreases when you call the Close () method, which provides a form object that automatically removes it from its Control Control collection.

Disposing a control simply destroys the native resources of the Windows operating system used by the control. The Handle property is the most obvious. Otherwise, the .NET object will not disappear, which requires the garbage collector to run. Saving a link to a custom control is a bad idea, the GC will not destroy it as long as there is a link to it, and you tend to get an ObjectDisposedException when trying to use it.

+1
source

Source: https://habr.com/ru/post/1400013/


All Articles