如何在VB6中使用复选框数组

How to use checkbox array in VB6

大家好,我需要有关如何使用复选框数组的帮助。我有 4 个复选框:

如果我选中一个复选框,其他 3 个复选框将被禁用。当我取消选中复选框时,四个将启用。

Private Sub Check1_Click(Index As Integer)
   Dim i As Long, ChkCount As Long
   ChkCount = 0

   For i = 0 To 3
      If Check1(i).Value = 1 Then ChkCount = ChkCount + 1
   Next i

   For i = 1 To 3
      If ChkCount < 1 Then
         Check1(i).Enabled = True
      Else
         If Check1(i).Value = 0 Then Check1(i).Enabled = False
      End If
   Next i
End Sub

这是我的代码,但 check1(0) 没有禁用。


我修好了,谢谢大家的回复。 第 6 行出错,应该是 i = 0 到 3

Private Sub check1_Click(Index As Integer)

Select Case Index

Case 0
'Checks or unchecks the first checkbox and gives the other checkboxes the opposite values
check(0).Checked = Not check(0).Checked
check(1).Checked = Not check(0).Checked
check(2).Checked = Not check(0).Checked
check(3).Checked = Not check(0).Checked

Case 1
'Checks or unchecks the second checkbox and gives the other checkboxes the opposite values
check(1).Checked = Not check(1).Checked
check(0).Checked = Not check(1).Checked
check(2).Checked = Not check(1).Checked
check(3).Checked = Not check(1).Checked

Case 2
'Checks or unchecks the third checkbox and gives the other checkboxes the opposite values
check(2).Checked = Not check(2).Checked
check(0).Checked = Not check(2).Checked
check(1).Checked = Not check(2).Checked
check(3).Checked = Not check(2).Checked

Case 3
'Checks or unchecks the fourth checkbox and gives the other checkboxes the opposite values
check(3).Checked = Not check(3).Checked
check(0).Checked = Not check(3).Checked
check(1).Checked = Not check(3).Checked
check(2).Checked = Not check(3).Checked

End Select

End Sub

这是一个工作演示,我刚刚在我的 VB6 中测试过。

Option Explicit

Private Sub Check1_Click(Index As Integer)
    Dim i As Long
    Dim isDisable As Boolean
    isDisable = Not (Check1(Index).Value = 1)
    For i = 0 To Check1.Count - 1
        If i <> Index Then
            Check1(i).Enabled = isDisable
        End If
    Next i
End Sub