views:

186

answers:

1

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 invisible.

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();
    
A: 

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.

MPelletier
Disposing a Control will surely invalidate its screen area.
Henk Holterman
@MPelletier sir, that means dispose method is specially design or preapared only for form in case direct use. if it is use at runtime controls it's have to first get remove it from control and thenafter applied dispose method. In short dispose method's direct use for only forms.
mahesh
@MPelletier sir, and direct use of dispose method on other controls like textboxes which harmful we will lose other controls resources espacially listbox control. it is very dangerous to use it without knowing about it. and we have to pay for it damages.
mahesh
@mahesh: Form.close() usually takes care of all the disposing required. I will investigate further on how to remove a textbox properly.
MPelletier
@Henk Holterman Sir, I am totally agree with u
mahesh
I have found this question answer from some where else by two days before and i am satisfy with this my stackoverflow community thx all of u and i am apolozige against any bad thing if i created. Now if community wants to delete it than they are welcome to delete it. please.
mahesh
@MPelletier sir thx for ur co-operation
mahesh
But it's my Request that it is most important to knowing it to our new comer who don't aware of this type of problem. last Opinion i have leave up to our community.
mahesh
This is rectification against my first comment : The winforms are closed directly on called dispose method where it is not same with other controls due to lac of access.
mahesh