四、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
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Me.B_LOGIN = New System.Windows.Forms.Button
3Me.T_NAME = New System.Windows.Forms.TextBox
4Me.T_PASSWORD = New System.Windows.Forms.TextBox
5Me.L_NAME = New System.Windows.Forms.Label
6Me.L_PASSWORD = New System.Windows.Forms.Label
7Me.SuspendLayout()
8'
9'B_LOGIN
10'
11Me.B_LOGIN.Location = New System.Drawing.Point(112, 176)
12Me.B_LOGIN.Name = "B_LOGIN"
13Me.B_LOGIN.Size = New System.Drawing.Size(56, 32)
14Me.B_LOGIN.TabIndex = 0
15Me.B_LOGIN.Text = "登陆"
16'
17'T_NAME
18'
19Me.T_NAME.Location = New System.Drawing.Point(96, 64)
20Me.T_NAME.Name = "T_NAME"
21Me.T_NAME.Size = New System.Drawing.Size(88, 21)
22Me.T_NAME.TabIndex = 1
23Me.T_NAME.Text = ""
24'
25'T_PASSWORD
26'
27Me.T_PASSWORD.Location = New System.Drawing.Point(96, 120)
28Me.T_PASSWORD.Name = "T_PASSWORD"
29Me.T_PASSWORD.Size = New System.Drawing.Size(88, 21)
30Me.T_PASSWORD.TabIndex = 2
31Me.T_PASSWORD.Text = ""
32'
33'L_NAME
34'
35Me.L_NAME.Location = New System.Drawing.Point(24, 72)
36Me.L_NAME.Name = "L_NAME"
37Me.L_NAME.Size = New System.Drawing.Size(48, 16)
38Me.L_NAME.TabIndex = 3
39Me.L_NAME.Text = "用户名"
40'
41'L_PASSWORD
42'
43Me.L_PASSWORD.Location = New System.Drawing.Point(24, 120)
44Me.L_PASSWORD.Name = "L_PASSWORD"
45Me.L_PASSWORD.Size = New System.Drawing.Size(64, 16)
46Me.L_PASSWORD.TabIndex = 4
47Me.L_PASSWORD.Text = "密 码"
48'
49'login
50'
51Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
52Me.ClientSize = New System.Drawing.Size(292, 273)
53Me.Controls.Add(Me.L_PASSWORD)
54Me.Controls.Add(Me.L_NAME)
55Me.Controls.Add(Me.T_PASSWORD)
56Me.Controls.Add(Me.T_NAME)
57Me.Controls.Add(Me.B_LOGIN)
58Me.Name = "login"
59Me.Text = "login"
60Me.ResumeLayout(False)
61
62End Sub
63
64#End Region
65
66Private Sub B_LOGIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_LOGIN.Click
67user = New business.login.UserLimits
68Dim up As business.login.UserParameter
69
70up = Me.user.GetUserObj(Me.T_NAME.Text, Me.T_PASSWORD.Text)
71If (Not up Is Nothing) Then
72MsgBox("user id is:" + up.get_ID)
73'用户验证成功,进入主窗口
74myForm.LogInUser = up
75myForm.LogForm = Me.ActiveForm
76Dim mainform As New WinForm.Main.MainWindow
77myForm.MainForm = mainform
78Me.Hide()
79'Me.Close()
80mainform.Show()
81Else
82MsgBox("wrong user!")
83End If
84End Sub
85End Class
86可以看到,除了Form自动生成的代码以为,开发者要自己写的代码相对较少了。
87
88总结:细心的读者可以发现,采用这种体系结构后,几乎每个功能的实现的代码都是很类似的,而且代码量大,对于这种特点,我们采用“宏”这个工具,来自动生成四层中的除了WinForm以外的代码,这样就使得开发变得比较容易,工作量大为减少。在后续的文章中 ,会有对“宏”的描述。</system.diagnostics.debuggerstepthrough()>