Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am having problems with check boxes

Status
Not open for further replies.

kemsu

Programmer
Aug 8, 2002
4
0
0
US
I am writing an application in which I use a control array of checkboxes. I am having serious difficulty detecting when each checkbox has been selected or deselected. Please anybody help???
 
The following is used on a form with 5 check boxes (named chkTest(0) thru chkTest(4)) and 1 command button (cmdReadBoxes). When the command button is clicked, the value of true or false is stored in a module level variable (blnIsChecked) to indicate if a particular box (0-4) is checked. The biggest problem I have seen people have with checkboxes is trying to read the value property as True or False rather than comparing to the constants vbChecked and vbUnChecked.



Option Explicit

Dim blnIsChecked(4) As Boolean

Private Sub cmdReadBoxes_Click()

Dim intIndex As Integer

For intIndex = chkTest.LBound To chkTest.UBound
If chkTest(intIndex).Value = vbChecked Then
blnIsChecked(intIndex) = True
Else
blnIsChecked(intIndex) = False
End If
Next

End Sub

 
When the state of a checkbox changes, the Check1_Click event is fired.
For an array, I usually store the state of all checkboxes in a Long (if less than 30 checkboxes in array), using the index property of the array to set bits within the Long

Try this:
In declarations:
Code:
Dim myChecks As Long
Const CheckMax As Long = 2 ^ 30 - 1
In code:
Code:
Private Sub Check1_Click(Index As Integer)
If Check1(Index).Value = 1 Then
myChecks = myChecks Or 2 ^ Index
Else
myChecks = myChecks And (CheckMax - 2 ^ Index)
End If
End Sub

To find out what's checked use:
Code:
Private Sub Command1_Click()
If myChecks And 2 ^ 4 Then
MsgBox "chk4 checked"
End If
End Sub
changing 2^4 to 2^n to get nth value

Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Private Sub CheckBox_Click(Index As Integer)
If CheckBox(Index) = 0 Then
'insert your not checked code here
Else
'insert your is checked code here
End If
End Sub

This is what I use to detect a change in a checkbox array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top