VS2005 Forms and Partial ClassesPartial Classes Introduced in VS2005 | | Where did the code for the form and its control creation go in VS2005. In previous versions of Visual Studio .NET (C# & VB.NET), I could view the code of my form and see the InitializeComponent Method (this method creates the form and its controls at run-time). In VS2005, the code doen't appear in the Public Class Form1, for example.
Well, that's partially true. In VS2005, we now have partial classes. Whereas there were 2 files associated with a Form in Visual Studio .NET (1.0 and 1.1), Form1.vb and Form1.resx, there are now three files.
Form1.vb still contains the code that you write for the form including form and control events, and your public and private methods. However the declaration of the Controls and the Form (i.e., InitializeComponent method) have been moved to another file named Form1.Designer.vb.
Actually, all this appears to do for me as a developer, is to keep me from having to look at all the code for the Form and Control creation. Normally, I am not interested in this code and it can clutter my view of the code in which I am interested, that being the code that I add to the form.
If you are interested in the way the new Designer file differs from the old Form1.vb, the following code snippets show the top of the two files.
Figure 1 - Form1.vb
This file now contains only the code that you write plus any Form or Control Events created by double-clicking on the form or a control. You will also note that this class no longer inherits from System.Windows.Forms.Form. That now appears in the new designer file.
Public Class Form1
''' <summary>
'''
'''
'''
'''
''' <remarks>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
''' <summary>
'''
'''
''' <remarks>
Public Sub test()
While True
End While
MsgBox("Test")
Dim s As String = String.Empty
End Sub
End Class
|
Figure 2 - Form1.Designer.vb
This new file contains the designer generated code that used to be in Form1.vb. Note the new modifier "Partial". As with the previous versions of Visual Studio .NET, you will not see this file or the .resx file in the Solution Explorer unless you click the "Show All Files" button at the top of the explorer. Therefore, it appears, at first notice that the designer code has disappeared.
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(157, 101)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
|
|