📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4695
ClientLeft = 60
ClientTop = 345
ClientWidth = 6210
LinkTopic = "Form1"
ScaleHeight = 4695
ScaleWidth = 6210
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdForEach
Caption = "测试For Each语句"
Height = 375
Left = 240
TabIndex = 24
Top = 4200
Width = 1575
End
Begin VB.CommandButton cmdOut
Caption = "输出资料"
Height = 375
Left = 3960
TabIndex = 23
Top = 4200
Width = 1695
End
Begin VB.TextBox txtName
Height = 375
Left = 3960
TabIndex = 22
Top = 3600
Width = 1695
End
Begin VB.TextBox txtNum
Height = 375
Left = 4680
TabIndex = 19
Top = 3120
Width = 975
End
Begin VB.Frame Frame2
Caption = "输出个人信息:"
Height = 2775
Left = 3000
TabIndex = 8
Top = 120
Width = 2655
Begin VB.TextBox txtOutSalary
Height = 375
Left = 1080
TabIndex = 18
Top = 2280
Width = 1455
End
Begin VB.TextBox txtOutSex
Height = 375
Left = 1080
TabIndex = 14
Top = 1680
Width = 1455
End
Begin VB.TextBox txtOutName
Height = 375
Left = 1080
TabIndex = 10
Top = 480
Width = 1455
End
Begin VB.TextBox txtOutAge
Height = 375
Left = 1080
TabIndex = 9
Top = 1080
Width = 1455
End
Begin VB.Label Label8
Caption = "工资:"
Height = 255
Left = 120
TabIndex = 17
Top = 2400
Width = 615
End
Begin VB.Label Label6
Caption = "姓名:"
Height = 255
Left = 120
TabIndex = 13
Top = 480
Width = 615
End
Begin VB.Label Label5
Caption = "年龄:"
Height = 255
Left = 120
TabIndex = 12
Top = 1080
Width = 615
End
Begin VB.Label Label4
Caption = "性别:"
Height = 375
Left = 120
TabIndex = 11
Top = 1680
Width = 615
End
End
Begin VB.Frame Frame1
Caption = "输入个人信息:"
Height = 2775
Left = 240
TabIndex = 1
Top = 120
Width = 2655
Begin VB.TextBox txtInSalary
Height = 375
Left = 1080
TabIndex = 16
Top = 2280
Width = 1455
End
Begin VB.ComboBox cboInSex
Height = 315
Left = 1080
TabIndex = 7
Top = 1680
Width = 1455
End
Begin VB.TextBox txtInAge
Height = 375
Left = 1080
TabIndex = 6
Top = 1080
Width = 1455
End
Begin VB.TextBox txtInName
Height = 375
Left = 1080
TabIndex = 5
Top = 480
Width = 1455
End
Begin VB.Label Label7
Caption = "工资:"
Height = 255
Left = 120
TabIndex = 15
Top = 2400
Width = 615
End
Begin VB.Label Label3
Caption = "性别:"
Height = 375
Left = 120
TabIndex = 4
Top = 1680
Width = 615
End
Begin VB.Label Label2
Caption = "年龄:"
Height = 255
Left = 120
TabIndex = 3
Top = 1080
Width = 615
End
Begin VB.Label Label1
Caption = "姓名:"
Height = 255
Left = 120
TabIndex = 2
Top = 480
Width = 615
End
End
Begin VB.CommandButton cmdIn
Caption = "输入信息"
Height = 495
Left = 240
TabIndex = 0
Top = 3120
Width = 1335
End
Begin VB.Label Label10
Caption = "要显示谁的资料?"
Height = 255
Left = 2280
TabIndex = 21
Top = 3600
Width = 1695
End
Begin VB.Label Label9
Caption = "要显示第几个人的资料?"
Height = 255
Left = 2280
TabIndex = 20
Top = 3120
Width = 2175
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private WithEvents clsTry As clsPerson '声明一个带事件的人员类
Attribute clsTry.VB_VarHelpID = -1
Dim colTry As New colPersons '定义一个模块级别的集合类
'事件监听过程,当类中引发事件的时候,调用该过程
'为客户程序传递信息。相当于将类中定义的事件在此处添加
'以实际的代码,在此加以完整的实现
Private Sub clsTry_DataError(ByVal errStr As String)
'此处传递的errStr,将是类内部触发事件时候,传入的字符串
'这样,如果类内引发该类型的事件比较多的时候,可以根据不同的
'字符串参数来确定是在类内的哪个部位触发了事件
MsgBox errStr, vbExclamation, "警告!"
End Sub
Private Sub cmdForEach_Click()
Dim sObj As clsPerson
Dim sNames As String
'简单测试该语句,连接每个集合那成员的姓名
'成为一个新的字符串
For Each sObj In colTry
sNames = sNames & " " & sObj.psnName
Next
MsgBox sNames
End Sub
Private Sub cmdIn_Click()
'对clsTry对象进行初始化,并且调用SetValue函数
'为该对象赋值。
Set clsTry = New clsPerson
clsTry.SetValue Trim$(txtInName.Text), CInt(Trim$(txtInAge.Text)), Trim$(cboInSex.Text)
'如果只读属性OcurErr为False,则表示没有异常事件发生
'设置存放薪水的私有变量
'将这个类加入到集合对象colTry中去
If clsTry.OcurErr = False Then
clsTry.SetSalary CDbl(Trim$(txtInSalary.Text))
'暂时使用人员姓名作为唯一的标志Key,这就要求不要输入同名字的数据
'只是测试用,实际中,应该用身份证号作为唯一的标志
colTry.Add clsTry.psnName, clsTry.psnAge, clsTry.psnSex, clsTry.psnName
End If
End Sub
'主要用来测试集合
Private Sub cmdOut_Click()
Dim i As Long
Dim strName As String
'取得一个索引号
If Trim$(txtNum.Text) <> vbNullString Then
i = CLng(Trim$(txtNum.Text))
'根据该索引来访问colTry集合中的对象
txtOutName.Text = colTry(i).psnName
txtOutAge.Text = colTry(i).psnAge
txtOutSex.Text = colTry(i).psnSex
txtOutSalary.Text = colTry(i).GetSalary
End If
'取得一个关键词,该关键词与人员名字应该一样
If Trim$(txtName.Text) <> vbNullString Then
strName = Trim$(txtName.Text)
'根据关键词来访问集合中对象
txtOutName.Text = colTry(strName).psnName
txtOutAge.Text = colTry(strName).psnAge
txtOutSex.Text = colTry(strName).psnSex
txtOutSalary.Text = colTry(strName).GetSalary
End If
End Sub
Private Sub Form_Load()
cboInSex.Clear
cboInSex.AddItem "Male"
cboInSex.AddItem "Female"
End Sub
Private Sub txtName_KeyPress(KeyAscii As Integer)
'确保输入关键词的时候,没有索引号
txtNum.Text = vbNullString
End Sub
Private Sub txtNum_KeyPress(KeyAscii As Integer)
'确保使用索引号的时候,没有关键词干扰
txtName.Text = vbNullString
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -