Page 1 of 1

A quick and convinient way to manage binary information Manage multiple checkboxes on a form, or any binary controls Rate Topic: ***** 1 Votes

#1 Nevermalchik  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 23-August 06

Post icon  Posted 24 August 2006 - 07:52 AM

Hi, You will find, attached to this message, a tutorial on how to manage binary value or controls. Let say that you have an array of 100 checkboxes on a form and each checkbox value is stored in a database. Instead of having 100 fileds, with this solution, you could have only one field.

Hopefully, that will be useful to you. Here is the Text version:

Assume you have a form with 4 checkboxes, one label, one textbox and 2 buttons

Control Name / Control Type
chkTest(1) / CheckBox
chkTest(2) / CheckBox
chkTest(3) / CheckBox
chkTest(4) / CheckBox
lblValue / Label
txtValue / TextBox
cmdDecode / Command Button
cmdExit / Command Button



‘Each time a checkbox is selected or unselected, calculate the base 2 value
Private Sub chkTest_Click(Index AS Integer)
	Dim iCount AS Integer				‘Generic Counter

	‘Browse each checkboxes
	For iCount = 1 TO chkTest.Count
		If chkTest(iCount).Value = vbChecked Then
			‘The box is selected, calculate the Base 2 value
		 	lblValue.Caption = lblValue.Caption + (2^iCount)
		End If 
	Next iCount
End Sub

‘Decode the value provided in the textbox	
Private Sub cmdDecode_Click()
	Dim iCount AS Integer				‘Generic Counter
	Dim iValue AS Integer				‘Hold the value to decode

	‘Insure a zero value in the label caption
	lblValue.Caption = “0”

	‘If the value in the textbox is invalid, exit the sub, this is a simple check,
	‘you should not the the user input alpha in the textbox
	If txtValue.Text=”” OR Val(txtValue.Text)<0 Then Exit Sub

	‘Assing the value
	iValue = Val(txtValue.Text)


	‘Browse each checkboxes (make sure all boxes are unselected)
	For iCount = 1 TO chkTest.Count
		chkTest(iCount).Value = vbUnchecked
	Next iCount
	
	‘Browse each checkboxes in reverse order
	For iCount = chkTest.Count To 1 Step –1
	 	‘If the difference between the current iValue and the 2^iCount is positive
	   	If iValue - (2 ^ iCount) >= 0 Then
			‘The current box index was selected
			chkTest (iCount).Value = vbChecked
			‘Remove the current ‘base 2 power’ from iValue
		   	iValue = iValue - (2 ^ iCount)
	   	End If
	Next iCount
End Sub

‘Quit the application
Private Sub cmdExit_Click()
	Dim sMsg As String
	
	sMsg = “Do you really want to quit the application?”
	If Msgbox(sMsg, vbQuestion + vbYesNo,”Base 2 Example”) = vbYes Then
		End
	End If 
End Sub


Attached File(s)


This post has been edited by Nevermalchik: 24 August 2006 - 10:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: A quick and convinient way to manage binary information

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 24 August 2006 - 09:15 AM

I'm glad to see that tutorials explaining such basic-but-yet-unknown-to-many techniques are being written.
:)

Just remember the next time, that try writing tutorials in TXT format.
That way a Search Engine can index portions of your tutorial and in turn helps in getting more page hits.

P.S : You can also check whether a checkbox has been enabled using the following code:
Assuming the variable having specific bits is var1

' To check if Checkbox 3 is Checked

If (var2 AND 4)=1 Then
  Msgbox "Checkbox 3 is checked"
End If



4 in Binary is 100.
Using the AND Operator, we can test for a specific bit(3rd position/checkbox in this case) and verify whether a checkbox has been checked.
This is simple and easier to do than by using iteration.
Was This Post Helpful? 0
  • +
  • -

#3 Nevermalchik  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 23-August 06

Posted 24 August 2006 - 10:04 AM

View Postborn2c0de, on 24 Aug, 2006 - 09:15 AM, said:

I'm glad to see that tutorials explaining such basic-but-yet-unknown-to-many techniques are being written.
:)

Just remember the next time, that try writing tutorials in TXT format.
That way a Search Engine can index portions of your tutorial and in turn helps in getting more page hits.

P.S : You can also check whether a checkbox has been enabled using the following code:
Assuming the variable having specific bits is var1

' To check if Checkbox 3 is Checked

If (var2 AND 4)=1 Then
  Msgbox "Checkbox 3 is checked"
End If



4 in Binary is 100.
Using the AND Operator, we can test for a specific bit(3rd position/checkbox in this case) and verify whether a checkbox has been checked.
This is simple and easier to do than by using iteration.


I never though about that...Good call man, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1