请问这个类该如何写
各位老师:
以下为两段过程不同之外是对不同的控件的TabIndex属性进行设置,我想写成一个类,我想实现的目的是仅传递一个控件的类型过程就过可以,不要对不同类型的控件写不同的过程,谢谢!
'设置控件数组的TabIndex属性
public ctlTemp as object
Public Sub SetControlTabIndex(ctlTemp as object,iStart As Integer, iEnd As Integer)
Dim i%
With objFrm '窗体变量(dim objFrm as Form)
For i = iStart To iEnd
With .ctlTemp(i)
.TabIndex = i
End With
Next i
.ctlTemp(iEnd).TabStop = True
End With
End Sub
下列代码测试正确
'设置文本框控件数组的TabIndex属性
Public Sub SetTextBoxTabIndex(iStart As Integer, iEnd As Integer)
Dim i%
With objFrm
For i = iStart To iEnd
With .txtTemp(i)
.TabIndex = i
End With
Next i
.txtTemp(iEnd).TabStop = True
End With
End Sub
'设置组合框控件数组的TabIndex属性
Public Sub SetComboBoxTabIndex(iStart As Integer, iEnd As Integer)
Dim i%
With objFrm
For i = iStart To iEnd
With .cboTemp(i)
.TabIndex = i
End With
Next i
.cboTemp(iEnd).TabStop = True
End With
End Sub
[解决办法]
可以参考一下这个:
Public Sub SetControlTabIndex(ctlName As String)
Dim i%, ctl As Control, s As String
With objFrm '窗体变量(dim objFrm as Form)
For Each ctl In .Controls
If ctl.Name = ctlName Then s = s + ctl.Name '当是这个名称的控件数组时,进行处理,这里只取它们的名字相加(当然,名称其实都是一样的)。
Next
End With
MsgBox s
End Sub
Private Sub Form_Load()
SetControlTabIndex "Txt " 'TextBox控件数组名称
End Sub
[解决办法]
Public Sub SetControlTabIndex(ByVal objFrm As Form, ByVal Name As String, ByVal iStart As Integer)
Dim ctl As Control
For Each ctl In objFrm.Controls
If ctl.Name = Name Then
ctl.TabIndex = ctl.Index + iStart
End If
Next
End Sub
Private Sub Form_Load()
SetControlTabIndex Me, "Check1 ", 0
SetControlTabIndex Me, "Text1 ", 3
End Sub
[解决办法]
使用TypeOf关键字啊。
Public Sub SetControlTabIndex(ByVal objFrm As Form,ByVal iStart As Integer)
Dim ctl As Control
For Each ctl In objFrm.Controls
If TypeOf ctl Is TextBox Then
..........
ElseIf TypeOf ctl Is ComoBox Then
............
End If
Next
End Sub
[解决办法]
不要将单个控件和控件数组混淆起来!
用 For ... Each 访问的是单个控件,用 If ctl.Name = Name Then 保证可以遍历所有的同名控件(即控件数组的成员)。
要添加新的数组成员就不能用 For ... Each,要直接用 Load txtTmp(i)。