Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
    
like image 687
mahesh Avatar asked Sep 05 '10 10:09

mahesh


People also ask

What is the difference between form and user control?

User controls are a way of making a custom, reusable component. A user control can contain other controls but must be hosted by a form. Windows forms are the container for controls, including user controls. While it contains many similar attributes as a user control, it's primary purpose is to host controls.

Which window is used to design a form?

The easiest way to design a form is to use the Windows Forms Designer in Visual Studio . NET. The developer can use visual tools to lay out the form, with the designer translating the layout into Visual Basic .

What is form dispose?

This method invokes the Dispose method of each referenced object. Dispose will be called automatically if the form is shown using the Show method. If another method such as ShowDialog is used, or the form is never shown at all, you must call Dispose yourself within your application.

What is form in Visual Basic?

Visual Basic Form is the container for all the controls that make up the user interface. Every window you see in a running visual basic application is a form, thus the terms form and window describe the same entity. Visual Studio creates a default form for you when you create a Windows Forms Application.


1 Answers

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.

like image 121
MPelletier Avatar answered Oct 01 '22 02:10

MPelletier