Kent Maquirang
Kent Maquirang

Reputation: 5

How to use checkbox array in VB6

Hi guys I need help on how to use checkbox array. I have 4 checkboxes:

If I check one checkbox the other 3 will disable. And when I uncheck the checkbox the four will enable.

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

Here's my code but the check1(0) doesn't disable.


I fixed it guys thanks for response. got an error in line 6 it should be for i = 0 to 3

Upvotes: 0

Views: 1192

Answers (2)

sunsoft
sunsoft

Reputation: 566

This is an working demo, I just tested in my 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

Upvotes: 2

Ezani
Ezani

Reputation: 557

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

Upvotes: 0

Related Questions