Library tutorials & articles
Windows Form Designer generated code
- Introduction
- Disposing?
- A test application
- Controls and IDisposable
Introduction
components member generated by the designer and why it is not used in most of the
Forms he designed.
The components member is generated as part of the "Windows Forms Designer generated code" region, which is part of every Form created and managed by means of the Visual Studio .NET Windows Forms designer:
#Region " Windows Form Designer generated code "
...
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
...
#End Region
The code and the comments indicate that the components member is used by the Form to hold the list of components in order to be able to dispose the components as part of the Form.Dispose call.
I've checked the designer-generated code in all my .NET Windows Forms projects and I've realized that the components member is actually used only when a component having a specific constructor is placed onto a Form.
You might recall that a component is a class that implements the System.ComponentModel.IComponent interface (either directly or by deriving from a class that already implements the interface, such as System.ComponentModel.Component). If the component exposes a constructor with the specific signaturePublic Sub New(ByVal c As IContainer), then the components Form member is instantiated and passed to the component's constructor:
#Region " Windows Form Designer generated code "
...
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
...
Me.ImageList1 = _
New System.Windows.Forms.ImageList(Me.components)
...
#End Region
Within the New(ByVal c As IContainer) constructor, the component adds itself to the container by calling the IContainer.Add method. Within the Form.Dispose method the components.Dispose method is called ensuring that all the resources held by the Form's components are correctly released.
Related articles
Related discussion
-
Why choose c# when there is Java
by Thaj06 (0 replies)
-
Change color while typing in rich text box
by Akhtar Hussain (0 replies)
-
Third party components to enhance User Interface of Windows based application
by Shaila14041981 (0 replies)
-
VB.NET DataGridView Sort
by bradhen (0 replies)
-
About Comparison in Asp.Net
by S_Rahul (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
This thread is for discussions of Windows Form Designer generated code.