四、WinForm 层
WinForm 层主要完成与用户互动操作,以及传递参数等。代码相对简单,因为工作都在下面的基层实现了。
以处理用户登录的代码login.vb为例,给大家一个概要的印象。
login.vb代码如下:
Public Class login
Inherits System.Windows.Forms.Form
Protected user As business.login.UserLimits
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents T_NAME As System.Windows.Forms.TextBox
Friend WithEvents T_PASSWORD As System.Windows.Forms.TextBox
Friend WithEvents B_LOGIN As System.Windows.Forms.Button
Friend WithEvents L_NAME As System.Windows.Forms.Label
Friend WithEvents L_PASSWORD As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.B_LOGIN = New System.Windows.Forms.Button
Me.T_NAME = New System.Windows.Forms.TextBox
Me.T_PASSWORD = New System.Windows.Forms.TextBox
Me.L_NAME = New System.Windows.Forms.Label
Me.L_PASSWORD = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'B_LOGIN
'
Me.B_LOGIN.Location = New System.Drawing.Point(112, 176)
Me.B_LOGIN.Name = "B_LOGIN"
Me.B_LOGIN.Size = New System.Drawing.Size(56, 32)
Me.B_LOGIN.TabIndex = 0
Me.B_LOGIN.Text = "登陆"
'
'T_NAME
'
Me.T_NAME.Location = New System.Drawing.Point(96, 64)
Me.T_NAME.Name = "T_NAME"
Me.T_NAME.Size = New System.Drawing.Size(88, 21)
Me.T_NAME.TabIndex = 1
Me.T_NAME.Text = ""
'
'T_PASSWORD
'
Me.T_PASSWORD.Location = New System.Drawing.Point(96, 120)
Me.T_PASSWORD.Name = "T_PASSWORD"
Me.T_PASSWORD.Size = New System.Drawing.Size(88, 21)
Me.T_PASSWORD.TabIndex = 2
Me.T_PASSWORD.Text = ""
'
'L_NAME
'
Me.L_NAME.Location = New System.Drawing.Point(24, 72)
Me.L_NAME.Name = "L_NAME"
Me.L_NAME.Size = New System.Drawing.Size(48, 16)
Me.L_NAME.TabIndex = 3
Me.L_NAME.Text = "用户名"
'
'L_PASSWORD
'
Me.L_PASSWORD.Location = New System.Drawing.Point(24, 120)
Me.L_PASSWORD.Name = "L_PASSWORD"
Me.L_PASSWORD.Size = New System.Drawing.Size(64, 16)
Me.L_PASSWORD.TabIndex = 4
Me.L_PASSWORD.Text = "密 码"
'
'login
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.L_PASSWORD)
Me.Controls.Add(Me.L_NAME)
Me.Controls.Add(Me.T_PASSWORD)
Me.Controls.Add(Me.T_NAME)
Me.Controls.Add(Me.B_LOGIN)
Me.Name = "login"
Me.Text = "login"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub B_LOGIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_LOGIN.Click
user = New business.login.UserLimits
Dim up As business.login.UserParameter
up = Me.user.GetUserObj(Me.T_NAME.Text, Me.T_PASSWORD.Text)
If (Not up Is Nothing) Then
MsgBox("user id is:" + up.get_ID)
'用户验证成功,进入主窗口
myForm.LogInUser = up
myForm.LogForm = Me.ActiveForm
Dim mainform As New WinForm.Main.MainWindow
myForm.MainForm = mainform
Me.Hide()
'Me.Close()
mainform.Show()
Else
MsgBox("wrong user!")
End If
End Sub
End Class
可以看到,除了Form自动生成的代码以为,开发者要自己写的代码相对较少了。
总结:细心的读者可以发现,采用这种体系结构后,几乎每个功能的实现的代码都是很类似的,而且代码量大,对于这种特点,我们采用“宏”这个工具,来自动生成四层中的除了WinForm以外的代码,这样就使得开发变得比较容易,工作量大为减少。在后续的文章中 ,会有对“宏”的描述。