Full Version: A quick and convinient way to manage binary information
Dream.In.Code > Programming Tutorials > Visual Basic Tutorials
Nevermalchik
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



CODE

‘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

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

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:
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.
Nevermalchik
QUOTE(born2c0de @ 24 Aug, 2006 - 09:15 AM) *

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

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:
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!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.