Packing Check-Box Values into a Single Integer

Use this code to find the binary representation of the currently checked check boxes:

 Function WhichCheck(ctrl As Object) As _	Integer' This function returns the binary value ' of an array of controls where the ' value is 2 raised to the index of each ' checked control' ie element 0 : 2 ^ 0 returns 1'elements 0 and 2 : 2^0 + 2^2 returns 5	Dim i	Dim iHolder	' in case ctrl is not a valid object	'default to failure return =0 on 	'fail	On Error GoTo WhichCheckErr	' find the binary representation of 	' an array of check box controls	For i = ctrl.LBound To ctrl.UBound		If ctrl(i) = 1 Then			' if it is checked add in its 			' binary value			iHolder = iHolder Or 2 ^ i		End If	NextWhichCheckErr:	WhichCheck = iHolderEnd Function

Call the function with code like this:

 iCurChecked = WhichCheck(Check1)

Check1 is an array of check boxes, and iCurChecked is an integer. Here’s the “dual” routine that sets the state of all the check boxes in a control array given an Integer that holds their binary representation:

 Sub SetChecked(ctrl As Object, _	iCurCheck%)' This sub sets the binary value of an ' array of controls where iCurChecked is ' 2 raised to the index of each checked ' control	Dim i	' in case ctrl is not a valid object	On Error GoTo SetCheckErr	' use the binary representation to 	' set individual check box controls	For i = ctrl.LBound To ctrl.UBound		If iCurCheck And (2 ^ i) Then			' if it is checked add in its 			' binary value			ctrl(i).Value = 1		Else			ctrl(i).Value = 0		End If	NextSetCheckErr:End Sub

Call the sub with code like this:

 Call SetChecked(Check1, iDesired)

Check1 is an array of checkboxes, and iDesired is a binary representation of the desired settings.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS