Integer to 8 checkboxes checked

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 5468 Views - Last Post: 22 June 2012 - 04:22 PM Rate Topic: -----

#1 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Integer to 8 checkboxes checked

Posted 14 June 2012 - 04:52 PM

I am trying to take an integer value 0-255, and check 8 checkboxes based on the binary representation of the integer.

Firstly, I am generating the integer value based on the checkboxes the user selects with the code below.

    Private Sub chk_AltHold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_AltHold1.CheckedChanged, chk_AltHold2.CheckedChanged, chk_AltHold3.CheckedChanged, chk_AltHold4.CheckedChanged, chk_AltHold5.CheckedChanged, chk_AltHold6.CheckedChanged, chk_AltHold7.CheckedChanged, chk_AltHold8.CheckedChanged
        'Sc 3D Tx Setup
        Dim alt_val As Integer = 0
        Dim alt_valS As String

        If (chk_AltHold1.Checked) Then alt_val = alt_val + 1
        If (chk_AltHold2.Checked) Then alt_val = alt_val + 2
        If (chk_AltHold3.Checked) Then alt_val = alt_val + 4
        If (chk_AltHold4.Checked) Then alt_val = alt_val + 8
        If (chk_AltHold5.Checked) Then alt_val = alt_val + 16
        If (chk_AltHold6.Checked) Then alt_val = alt_val + 32
        If (chk_AltHold7.Checked) Then alt_val = alt_val + 64
        If (chk_AltHold8.Checked) Then alt_val = alt_val + 128

        alt_valS = alt_val.ToString.PadLeft(4, "0"c)
        txt_FnHtLock.Text = alt_valS
    End Sub


This converts the checkboxes checked into an integer 0-255 thats represenatative of an 8 bit binary number.

I need to do it in reverse. Take the integer and convert to binary and check the boxes accordingly.

I have searched endlessly, ad tried several different things in code to achieve this to no avail.

I am used to microcontroller programming in various languages, and the equivalent is easy for that, but in VB.net, I cant figure it out.

Any help is very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Integer to 8 checkboxes checked

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 05:05 PM

If your checkboxes were in a List<CheckBox> you could just loop through and check them as you loop through the string representation of of the binary.

Converting an int to a binary string only takes a line:
Dim i As Integer = 181
Dim s As String
s = i.ToString("X")
Console.Writeline(s)


So s is going to contain 10110101

Loop through the string character by character. If it is a one then tick the corresponding checkbox.

First element of the string corresponds to the first element of your List<Checkbox> and so on.

Reversing the binary to int is just as easy
String binaryString = "1001";
int base = 2;
int decimal = Integer.parseInt(binaryString, base);

Was This Post Helpful? 1
  • +
  • -

#3 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 06:50 PM

Thanks for the quick reply. However, I have tried to integrate that method into what I have and it does not fit well. I have not been able to find a way to use a Checkbox list in a manner thats suitable for the GUI. If in fact you meant to use a Checkboxlist.

However, your reply has given me an idea to try something else. I dont think it will work, but I will give it a shot.
Was This Post Helpful? 0
  • +
  • -

#4 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 06:59 PM

OH, "X" will format as Hex, not binary. That was one of the other issues I was having. I dont see a binary formatter on MSDN
Was This Post Helpful? 0
  • +
  • -

#5 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 07:16 PM

Ok, I figured out how to get the binary string

Dim i as integer = 255
Dim s as string
s = Convert.ToString(i, 2)


This returns 11111111 as a string.

Now to get those bits to check the corresponding checkbox!!
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 07:54 PM

I meant the exact type I said: List<CheckBox>

Are you familiar with the List<T> class? Its like an advanced Array

YOur string has the same number of elements as your List<> of Checkboxes: 8

So looping through the list would make the the first element of the List<> correspond to the first element of the string.

Give it a try and show use your code effort.

It is 2150hrs - I'm going to give it a go in C# as that's my language and post my time here. After you have shown your effort to use my suggestion, if you need some help I'll show you how I did it. But you have to show your effort first - That's how we do it here.
Was This Post Helpful? 1
  • +
  • -

#7 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 08:09 PM

Ok, I have something that works, but not really the way it should be.

        Dim sens_config As Integer = Integer.Parse(txt_SensConfig.Text)
        Dim str As String
        Dim singleChar1 As Char
        Dim int1 As Integer = Nothing

        str = Convert.ToString(sens_config, 2)      'Convert to binary as 10110010 etc.  Works fine!!

        TextBox1.Text = str                         'Display the binary for debugging...OK

        singleChar1 = str.Chars(0)                  'Get the Char at index 0 .....OK

        TextBox2.Text = singleChar1                 'Display the indexed Char for debugging .....OK


        int1 = Convert.ToInt32(singleChar1)         'Convert the Char to an integer so it can be tested...OK

        TextBox3.Text = int1                        'Display converted int1 for debugging.DISPLAYS Char Code 49!!

        If int1 = 49 Then chk_Baro.Checked = True


When I convert the 0 or 1 from the string, it returns the ASCII code for the Char. Which I can use, but I would rather just test if its 1 or 0, but it will not allow me to test the returned string character. It will not work unless I convert to char code.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 08:14 PM

HINT: A string is a collection that you can iterate through. foreach

There is no need for Convert.
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 08:43 PM

So here's what I built:

Attached Image

This is one of the rare times when I kept the designer names of CheckBox1, CheckBox2 ... because in this case they actually made sense.

Here is a hint: The breakdown of the fields, properties and methods used.
Attached Image

Now if you want to look at the C# and use it as base to build your VB.NET just open the spoiler tag below.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#10 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 08:56 PM

Thanks for your help!! The way I have it now works, so I'm going to use it I guess. I know its not the correct way....but it works!!

I know how to iterate through the string, but I do not understand how to test each and check the box accordingly with that method.

I see your code now. Thats pretty much exactly what im looking for. I will have a go at figuring out how to port it to VB.

THANKS!!!!
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 08:58 PM

Look at the for loop of lines 25 to 29

We are working our way through the string, from left to right.
If the first element of the string is a '1' then we check the Corresponding CheckBox in our List<>

if position is 4 then its the 5th element (char) of the string and the 5th element of our CheckBox collection.
Was This Post Helpful? 1
  • +
  • -

#12 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 09:37 PM

Ok, I have the For Each working as below

  Dim sens_config As Integer = Integer.Parse(txt_SensConfig.Text)
        Dim str As String

        str = Convert.ToString(sens_config, 2)

        For Each digit As Char In str
            Debug.WriteLine(digit)
        Next


This writes the digits properly to the debug window. Now I just need to figure out how to test each one accordingly.
Was This Post Helpful? 0
  • +
  • -

#13 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 14 June 2012 - 11:46 PM

Ok, this is what I came up with. It works. The string was reversed, so instead of reversing the string I listed the checkboxes in the array in reverse and it comes out correct.

What do you think about doing it this way

        Dim chk_sensor() As CheckBox = New CheckBox() {chk_Sens8, chk_Sens7, chk_Sens6, chk_Sens5, chk_Sens4, chk_Sens3, chk_Sens2, chk_Sens1}

        Dim sens_config As Integer = Integer.Parse(txt_SensConfig.Text)
        Dim str As String

        str = Convert.ToString(sens_config, 2)

        Dim i As Integer
        For i = 0 To 7
            Dim test1 As Boolean = str.Substring((i), 1).Equals("1")
            If test1 = True Then chk_sensor(i).Checked = True
        Next

Was This Post Helpful? 1
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 15 June 2012 - 12:01 AM

You've broken down some of the things I had in one line into 2 or 3 but that's fine and makes it easier to read and place breakpoints.

Well done! Its great to see someone work through a scenario and learn from it. So many of our rookies just want us to give them completed code. Keep up the good work ethic!
Was This Post Helpful? 0
  • +
  • -

#15 KKUSA  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-June 12

Re: Integer to 8 checkboxes checked

Posted 15 June 2012 - 12:32 AM

THANKS!! I like to know how to do things, not have someone do them for me, but sometimes I need a nudge in the right direction.

However, I do have one issue occasionally and Im not sure why its behaving this way.

The code below
        Dim i As Integer
        For i = 0 To 7
            Dim test1 As Boolean = str.Substring((i), 1).Equals("1")
            If test1 = True Then chk_sensor(i).Checked = True
        Next


Sometimes its throwing an, ArgumentOutOfRange Exception. Index and length must refer to a location within the string.
Parameter name: length

It obviously means that its trying to use an index value outside of 0-7, but how is that possible? It should always reset to zero each time the For is called, and only count to 7 no matter what shouldnt it? It does not do it all the time. Should I dim the i integer to 0?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »