我有很多名称非常相似的对象,我想知道是否有任何方法可以将对象与数字组合在一起。
我找不到任何方法。
Dim chkBox As Object
For i = 0 To 3
For n = 1 To 27
chkBox = chkKeypadModule & n
If (chkBox.Checked = True) And (aryNumbers(0) <> 1) And (aryNumbers(1) <> 1) And (aryNumbers(2) <> 1) And (aryNumbers(3) <> 1) Then
aryNumbers(Index) = 1
ElseIf (chkKeypadModule2.Checked = True) And (aryNumbers(0) <> 2) And (aryNumbers(1) <> 2) And (aryNumbers(2) <> 2) And (aryNumbers(3) <> 2) Then
End If
Next n
Next i
我希望能够运行for循环并根据索引更改要查看的对象。似乎没有办法
答案 0 :(得分:1)
是的,使用数组。
尝试以下代码(我简化了代码,因为您似乎有很多多余的代码):
Dim chkKeypadModules = { chkKeypadModule1, chkKeypadModule2, chkKeypadModule3, chkKeypadModule4, chkKeypadModule5, chkKeypadModule6, chkKeypadModule7, chkKeypadModule27 }
Dim chkBox As CheckBox
For n = 0 To chkKeypadModules.Length - 1
chkBox = chkKeypadModules(n)
If chkBox.Checked Then
aryNumbers(Index) = 1
End If
Next n
您需要确保在数组中包含所有chkKeypadModule1{n}
。
答案 1 :(得分:0)
您可以使用Controls.Find
使用以下内容搜索控件:
For i As Integer = 0 To 3
For n As Integer = 1 To 27
Dim arrCtrl() As Control = Me.Controls.Find("chkKeypadModule" & n, True)
If arrCtrl.Length = 1 Then
chkBox = arrCtrl(0)
If chkBox.Checked AndAlso aryNumbers(0) <> n AndAlso aryNumbers(1) <> n AndAlso aryNumbers(2) <> n AndAlso aryNumbers(3) <> n Then
aryNumbers(i) = n
End If
End If
Next
Next