📄 frmcollection.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "集合测试"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 5970
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 5970
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdName
Caption = "按名称访问"
Height = 495
Left = 3840
TabIndex = 7
Top = 2280
Width = 1455
End
Begin VB.CommandButton cmdIndex
Caption = "按序号访问"
Height = 495
Left = 2160
TabIndex = 6
Top = 2280
Width = 1335
End
Begin VB.TextBox txtInput
Height = 375
Left = 2640
TabIndex = 5
Text = "txtInput"
Top = 1440
Width = 1335
End
Begin VB.TextBox Text3
Height = 375
Left = 3120
TabIndex = 3
Text = "Text3"
Top = 240
Width = 1215
End
Begin VB.TextBox Text2
Height = 375
Left = 1800
TabIndex = 2
Text = "Text2"
Top = 240
Width = 975
End
Begin VB.TextBox Text1
Height = 375
Left = 240
TabIndex = 1
Text = "Text1"
Top = 240
Width = 1095
End
Begin VB.CommandButton cmdClear
Caption = "清空文本框"
Height = 495
Left = 360
TabIndex = 0
Top = 2280
Width = 1455
End
Begin VB.Label lblInput
Caption = "输入文本框序号或名称:"
Height = 375
Left = 240
TabIndex = 4
Top = 1440
Width = 2055
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim colTextBoxes As New Collection
Private Sub cmdClear_Click()
Dim ctlItem As Control
For Each ctlItem In colTextBoxes
ctlItem.Text = ""
Next ctlItem
End Sub
Private Sub cmdIndex_Click()
On Error GoTo err
'MsgBox colTextBoxes.Item(CInt(Trim(txtInput.Text))).Text
MsgBox colTextBoxes(CInt(Trim(txtInput.Text))).Text
Exit Sub
err:
MsgBox err.Description
End Sub
Private Sub cmdName_Click()
On Error GoTo err
'MsgBox colTextBoxes![Text2].Text '用![]来访问集合成员也是可以的
'只是,此时[]内的内容不能有双引号,必须是特定的Key
'下面两条语句,通过()和特定的Key来访问集合成员,省略Item关键字和不
'省略有同样的效果
'MsgBox colTextBoxes.Item(Text2).Text
'MsgBox colTextBoxes(Text2).Text
'下面两条语句是更加灵活的方式,通过变量传递Key,从而实现访问集合成员
'MsgBox colTextBoxes.Item(Trim(txtInput.Text)).Text
MsgBox colTextBoxes(Trim(txtInput.Text)).Text
Exit Sub
err:
MsgBox err.Description
End Sub
Private Sub Form_Initialize()
Dim ctlItem As Control
For Each ctlItem In Me.Controls
'集合中只加入文本框控件
If TypeName(ctlItem) = "TextBox" Then
colTextBoxes.Add ctlItem, ctlItem.Name
End If
Next ctlItem
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -