【发布时间】:2020-10-22 08:14:41
【问题描述】:
! https://i.stack.imgur.com/RYIGs.png
目前,DDi 和 DDj,如果满足条件将显示消息“请为 Device1、2 或 3 选择一个设备通道。”、“请为 Device1、2 或 3 选择不同的设备通道”。我想让它说通道的实际名称,而不是 Device1、Device2 等。我不知道如何将数组放入我现有的结构中。我从下面开始,但在 for 开始的地方有语法错误。
If (HTSelection.DeviceDropDown1.List(0)) <> Empty Then
Else
DeviceDropDown1.AddItem "Device A: HT 1"
DeviceDropDown1.AddItem "Device A: HT 2"
DeviceDropDown1.AddItem "Device A: HT 3"
DeviceDropDown1.AddItem "Device A: HT 4"
DeviceDropDown1.AddItem "Device A: HT 5"
DeviceDropDown1.AddItem "Device A: HT 6"
DeviceDropDown1.AddItem "Device A: HT 7"
DeviceDropDown1.AddItem "Device A: HT 8"
DeviceDropDown1.AddItem "Device B: HT 1"
DeviceDropDown1.AddItem "Device B: HT 2"
DeviceDropDown1.AddItem "Device B: HT 3"
DeviceDropDown1.AddItem "Device B: HT 4"
DeviceDropDown1.AddItem "Device B: HT 5"
DeviceDropDown1.AddItem "Device B: HT 6"
DeviceDropDown1.AddItem "Device B: HT 7"
DeviceDropDown1.AddItem "Device B: HT 8"
DeviceDropDown1.AddItem "Channel_Not_Available"
End If
End Sub
Private Sub HTNextButton_Click()
Dim DDi(1 To 3) As String
DDi(1) = "Temperature"
DDi(2) = "Adapter"
DDi(3) = "USB"
Dim i As Integer
Dim DDj(1 To 3) As String
DDj(1) = "Temperature"
DDj(2) = "Adapter"
DDj(3) = "USB"
Dim ii As Integer
'DDi = 1
'DDj = 1
Numberflag = 0
DeviceFlagA = 0
DeviceFlagB = 0
'For DDi = 1 To 3
For i = 1 To 3
Device = Me.Controls.Item("DeviceDropDown" & DDi)
If Device = "Channel_Not_Available" Then
ElseIf Device = "Select Device" Then
MsgBox "Please select a number channel for" & DDi, vbCritical, "Error"
Exit For
Else
If InStr(1, Device, "Device A") Then
DeviceFlagA = 1
End If
If InStr(1, Device, "Device B") Then
DeviceFlagB = 1
End If
'For DDj = 1 To 3
For ii = 1 To 3
If DDi <> DDj Then
Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
If Device1 = "Channel_Not_Available" Then
Else
If Device = Device1 Then
MsgBox "Please select different number channel for" & DDj, vbCritical, "Error"
Numberflag = Numberflag + 1
Exit For
End If
End If
End If
Next
If Numberflag >= 1 Then
Exit For
End If
End If
Next
End If
End Sub
【问题讨论】:
-
去掉那个
On Error Resume Next...它隐藏了潜在的错误。 -
删除
on error resume next只会让事情复杂化,这里有一大堆问题都被该行隐藏了。DDi和DDj是字符串而不是整数,for行需要为i分配一个起始值。Device = Me.Controls.Item("DeviceDropDown" & DDi)也有问题。 -
将删除
On error Resume Next。i should be1 的起始值。我是新手,不知道如何将数组与我的 for 集成。Device = Me.Controls.Item在我不尝试使用数组时工作。 -
我假设
Device = Me.Controls.Item应该是 1、2 或 3,所以如果你需要数组中包含的字符串,你可以使用i,你需要用DDi(i)索引数组 -
Device在你的循环中不会改变,因为它引用了DDi,所以可能应该引用DDi(i)。