开发者

Difference in behaviours of the Dispose method between Windows Forms' Form and textboxes

开发者 https://www.devze.com 2023-01-15 09:28 出处:网络
Whenever I call the Dispose method on a Windows Forms form (derived from System.Windows.Forms.Form) to close it, the Dispose method finalizes by releasing resources and disposing the form.

Whenever I call the Dispose method on a Windows Forms form (derived from System.Windows.Forms.Form) to close it, the Dispose method finalizes by releasing resources and disposing the form.

I have runtime objects like textboxes as below:

Textbox Tb = new Textbox();

The user can create new textboxes dynamically. I want it so those textboxes that contain data stay around, and those that are null are removed. When I call the Dispose method on the empty textboxes, at runtime it looks like they're disposed, but generally they're just in开发者_运维技巧visible.

So:

  1. What is the difference between calling the dispose method on textboxes versus classes derived from Forms?

  2. Why is a Form disposed on calling e.g. Form1.Dispose();, and why not textboxes at runtime as below?

    if (tb.text=="")
        tb.Dispose();
    


From what I understand, it's because of ownership. The form owns the controls, so if you dispose of the controls, fine, you just need to refresh it. If you dispose of the form itself, it's gone, nothing to refresh.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号