Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,439 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,452 people online right now. Registration is fast and FREE... Join Now!




A quick and convinient way to manage binary information

 
Reply to this topicStart new topic

> A quick and convinient way to manage binary information, Manage multiple checkboxes on a form, or any binary controls

Rating  5
Nevermalchik
Group Icon



post 24 Aug, 2006 - 06:52 AM
Post #1


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



This post has been edited by Nevermalchik: 24 Aug, 2006 - 09:36 AM


Attached File(s)
Attached File  A_quick_and_convinient_way_to_manage_binary_information.doc ( 29k ) Number of downloads: 179
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 24 Aug, 2006 - 08:15 AM
Post #2
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.
Go to the top of the page
+Quote Post

Nevermalchik
Group Icon



post 24 Aug, 2006 - 09:04 AM
Post #3
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!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 10:35AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month