当从“新建项目”对话框中选择“Windows 应用程序”时,将创建一个解决方案,其中包含一个 Visual Basic 项目。此项目包含一些引用和一个类,该类称为 Form1.vb。
注意 若要查看默认情况下导入的命名空间,请参见“项目属性”对话框。若要访问此对话框,请右击解决方案资源管理器中的项目,选择“属性”,然后从左侧面板中选择“导入”。
如果查看此类中的默认代码,将看到以下内容:
Public ClassForm1InheritsSystem.Windows.Forms.Form
注意 默认情况下,以下代码将包含在一个区域中。
#Region " Windows Forms Designer generated code "Public Sub New()MyBase.New()" This call is required by the Windows Forms Designer.InitializeComponent()" Add any initialization after the InitializeComponent() call.End Sub" Form overrides dispose to clean up the component list.Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)If disposing ThenIf Not (components Is Nothing) Then components.Dispose()End IfEnd IfMyBase.Dispose(disposing) End Sub " Required by the Windows Forms Designer. Private components As System.ComponentModel.Container " NOTE: The following procedure is required by the Windows Forms Designer. " It can be modified using the Windows Forms Designer. " Do not modify it using the Code Editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()components = New System.ComponentModel.Container()Me.Text = "Form1" End Sub #End RegionEnd Class
定义
- InitializeComponent:由开发环境用来持久保存在 Windows 窗体设计器中设置的属性值。在 Visual Basic 的早期版本中,此信息不另存为代码,而是另存为 .frm 文件顶部的文本语句,开发人员始终看不到这些语句。
- Public Sub New():类构造函数。虽然您可以将窗体的初始化代码放置在这里,但是最佳位置是将初始化代码放置在窗体的 Load 事件中。
注意 在 Visual Basic 的先前版本中,可以在 Form_Initialize 事件过程中插入窗体的初始化代码。构造函数会替换此事件。当构造函数开始执行时,所有控件都已放置在窗体上并已初始化。
使用“属性”窗口可查看并操作以下内容:类名称,窗体所继承的类的名称,访问级别(Public、Private、Friend、Protected),其他窗体是否可从此窗体继承,以及窗体可能具有的任何特殊属性。

