Integer to 8 checkboxes checked

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

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

#16 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 15 June 2012 - 07:47 AM

When it breaks on line 3 with that error, double check the length of str

If you aren't left padding the string then 00000001 becomes 1, which obviously doesn't have a length of 8

tlhIn`toq`s code said:

69 Binary = Convert.ToString(temp, 2).PadLeft(8, '0');

This post has been edited by tlhIn`toq: 15 June 2012 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

#17 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:01 PM

Ahhh, Duh!!! I see it now. The only time it doesnt throw the error is when chr 7 (8) is 1, which would mean there is 8 chars. If its 0, no padding, it could be less than 8 chars., it errors.

GREAT!!! Thanks!!!

Sad thing is I knew that, as I had the same problem in another section of the code and figure it out!! Stupid me!!
Was This Post Helpful? 0
  • +
  • -

#18 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 16 June 2012 - 09:01 PM

Again, thanks for your help. I have it working great. However, I have 10 sets of 8 checkboxes I need to perform the same function on. Do you have any ideas how I could reduce the code and make it more efficient instead of having a separate section for each set?

Here is the code for 2 of them, which works fine, I just think a whole 10 of these is a bit inefficient if there is a better 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)
        str = str.ToString.PadLeft(8, "0"c)
        Dim i As Integer
        For i = 0 To str.Length - 1
            Dim test1 As Boolean = str.Substring((i), 1).Equals("1")
            If test1 = True Then chk_sensor(i).Checked = True
        Next

        Dim chk_AutoTx() As CheckBox = New CheckBox() {chk_AutoTX8, chk_AutoTX7, chk_AutoTX6, chk_AutoTX5, chk_AutoTX4, chk_AutoTX3, chk_AutoTX2, chk_AutoTX1}
        Dim AutoTx_config As Integer = Integer.Parse(txt_FnAuto.Text)
        str = Convert.ToString(AutoTx_config, 2)
        str = str.ToString.PadLeft(8, "0"c)
        For i = 0 To str.Length - 1
            Dim test1 As Boolean = str.Substring((i), 1).Equals("1")
            If test1 = True Then chk_AutoTx(i).Checked = True
        Next


Thanks again!!
Was This Post Helpful? 0
  • +
  • -

#19 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 16 June 2012 - 09:06 PM

UserControls. Period.

Any time you have the same functionality to be reused, you are probably looking at UserControls.

You make a UserControl that is your checkboxes and textboxes that perform this task. Then you just drag 10 of them out onto your form just as if they were 10 buttons.

I wrote these tutorials with this very concept in mind.

Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 1
  • +
  • -

#20 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 16 June 2012 - 10:13 PM

Thanks, I will have a go at creating a control out of it!!
Was This Post Helpful? 0
  • +
  • -

#21 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 18 June 2012 - 03:34 PM

First of all, thanks for all your help!!! It is much appreciated!!


I do have another issue I was wondering if you could help me with.

I need to uncheck boxes based on the checked status of other boxes. I have tried the below code, however it does not work properly. There does not seem to be an easy way to do it in vb.net. You would think there would be. I set it up to handle on every mouse click so it gets called whenever a box is clicked, the handle fires fine, but the boxes do not uncheck as expected. I have looked on the web trying to find a solution, but none I found work properly either.

    Private Sub Check_Combo() Handles Me.MouseClick

        If chk_AutoTX1.Checked = True And chk_3D1.Checked = True Then chk_3D1.Checked = False
        If chk_3D1.Checked = True And chk_AutoTX1.Checked = True Then chk_AutoTX1.Checked = False

    End Sub


The suggestion I encounter the most on the web is to use radio buttons, but that will no work as I must have more than one checked at a time, but they are conditional.

Any help is much appreciated!!
Was This Post Helpful? 0
  • +
  • -

#22 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 18 June 2012 - 03:51 PM

Quote

I need to uncheck boxes based on the checked status of other boxes. I have tried the below code, however it does not work properly.

What do you mean by not working write? What does it do versus what did you expect?

try something closer to this. It eliminates the if and the = true, which is unneeded and redundent

chk_3D1.Checked = !(chk_AutoTX1.Checked && chk_3D1.Checked)
chk_AutoTX1.Checked = !(chk_AutoTX1.Checked && chk_3D1.Checked)


In VB that might be closer to
chk_3D1.Checked = !(chk_AutoTX1.Checked And chk_3D1.Checked)
chk_AutoTX1.Checked = !(chk_AutoTX1.Checked And chk_3D1.Checked)


But, since you are treating these two checkboxes like radiobuttons (only one or the other can be ticked) then why don't you use RadioButtons? They control themselves and you don't don't micromanage this behavior. I'm pretty sure there is a style you can set for them to look like checkboxes, just for appearance sake if you are married to that look. But since they act like radiobuttons they should look like radio buttons because Windows users like things to follow the standard look and feel rules.
Was This Post Helpful? 0
  • +
  • -

#23 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 18 June 2012 - 04:11 PM

View PosttlhIn`toq, on 18 June 2012 - 03:51 PM, said:

Quote

I need to uncheck boxes based on the checked status of other boxes. I have tried the below code, however it does not work properly.

What do you mean by not working write? What does it do versus what did you expect?

try something closer to this. It eliminates the if and the = true, which is unneeded and redundent

chk_3D1.Checked = !(chk_AutoTX1.Checked && chk_3D1.Checked)
chk_AutoTX1.Checked = !(chk_AutoTX1.Checked && chk_3D1.Checked)


In VB that might be closer to
chk_3D1.Checked = !(chk_AutoTX1.Checked And chk_3D1.Checked)
chk_AutoTX1.Checked = !(chk_AutoTX1.Checked And chk_3D1.Checked)


But, since you are treating these two checkboxes like radiobuttons (only one or the other can be ticked) then why don't you use RadioButtons? They control themselves and you don't don't micromanage this behavior. I'm pretty sure there is a style you can set for them to look like checkboxes, just for appearance sake if you are married to that look. But since they act like radiobuttons they should look like radio buttons because Windows users like things to follow the standard look and feel rules.



The behavior I get is if I check box1, then I check box2 , box1 unchecks...OK , however, if I check box2, then check box1 , box2 does not uncheck.

I will give that a shot.

This applies to the previous 8 boxes to binary boxes, I dont think radio buttons will work for that. Out of a list of 8 radio buttons, only one can be checked at any time, regardless of appearance....Correct? I could be wrong however.
Was This Post Helpful? 0
  • +
  • -

#24 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 18 June 2012 - 04:17 PM

Just tried it the way you posted as below. I had to modify it to get it to run. Same exact result as above. Actually I think its the same expression, just coded differently.

  chk_3D1.Checked = (chk_AutoTX1.Checked = False And chk_3D1.Checked = True)
        chk_AutoTX1.Checked = (chk_AutoTX1.Checked = True And chk_3D1.Checked = False)

Was This Post Helpful? 0
  • +
  • -

#25 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 18 June 2012 - 04:23 PM

Yes, it is the same only different.

I still have to ask though... Same behavior as before means what exactly: I don't know what is supposed to be right and what it is doing now that is wrong.
Was This Post Helpful? 0
  • +
  • -

#26 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 18 June 2012 - 04:52 PM

The 2 check boxes need to be "either or". Only one of the two can be checked,like radio buttons, but radio buttons will not work in this case. The above code still allows both to be checked under all but one case.

The only time it will ensure only one is checked is if I check AutoTx1 first then 3D1. If I check 3D1 first then AutoTx1, they both remain checked.

Actually, I think I just thought of a way to do it in the code that adds the boxes up.

Do you think I can add a check in this code to see if another box is checked, and if so, to NOT add that button?

    Private Sub chk_AutoTX_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_AutoTX1.CheckedChanged, chk_AutoTX2.CheckedChanged, chk_AutoTX3.CheckedChanged, chk_AutoTX4.CheckedChanged, chk_AutoTX5.CheckedChanged, chk_AutoTX6.CheckedChanged, chk_AutoTX7.CheckedChanged, chk_AutoTX8.CheckedChanged
        'Sa Auto Level Tx Setup
        Dim auto_val As Integer = 0
        Dim auto_valS As String

        If (chk_AutoTX1.Checked) Then auto_val = auto_val + 1
        If (chk_AutoTX2.Checked) Then auto_val = auto_val + 2
        If (chk_AutoTX3.Checked) Then auto_val = auto_val + 4
        If (chk_AutoTX4.Checked) Then auto_val = auto_val + 8
        If (chk_AutoTX5.Checked) Then auto_val = auto_val + 16
        If (chk_AutoTX6.Checked) Then auto_val = auto_val + 32
        If (chk_AutoTX7.Checked) Then auto_val = auto_val + 64
        If (chk_AutoTX8.Checked) Then auto_val = auto_val + 128

        auto_valS = auto_val.ToString.PadLeft(4, "0"c)
        txt_FnAuto.Text = auto_valS
    End Sub



Maybe something like this? Look at first IF statement. I would obviously have to do this for every one.

    Private Sub chk_AutoTX_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_AutoTX1.CheckedChanged, chk_AutoTX2.CheckedChanged, chk_AutoTX3.CheckedChanged, chk_AutoTX4.CheckedChanged, chk_AutoTX5.CheckedChanged, chk_AutoTX6.CheckedChanged, chk_AutoTX7.CheckedChanged, chk_AutoTX8.CheckedChanged
        'Sa Auto Level Tx Setup
        Dim auto_val As Integer = 0
        Dim auto_valS As String

        If (chk_AutoTX1.Checked And chk_3D1.Checked = False) Then auto_val = auto_val + 1 And chk_AutoTX1.Checked = True And chk_3D1.Checked = False
        If (chk_AutoTX2.Checked) Then auto_val = auto_val + 2
        If (chk_AutoTX3.Checked) Then auto_val = auto_val + 4
        If (chk_AutoTX4.Checked) Then auto_val = auto_val + 8
        If (chk_AutoTX5.Checked) Then auto_val = auto_val + 16
        If (chk_AutoTX6.Checked) Then auto_val = auto_val + 32
        If (chk_AutoTX7.Checked) Then auto_val = auto_val + 64
        If (chk_AutoTX8.Checked) Then auto_val = auto_val + 128

        auto_valS = auto_val.ToString.PadLeft(4, "0"c)
        txt_FnAuto.Text = auto_valS
    End Sub

Was This Post Helpful? 0
  • +
  • -

#27 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 18 June 2012 - 05:38 PM

View PostKKUSA, on 18 June 2012 - 05:52 PM, said:

The only time it will ensure only one is checked is if I check AutoTx1 first then 3D1. If I check 3D1 first then AutoTx1, they both remain checked.


What if you just repeat the first check after the second check?

chk_3D1.Checked = (chk_AutoTX1.Checked = False And chk_3D1.Checked = True)
chk_AutoTX1.Checked = (chk_AutoTX1.Checked = True And chk_3D1.Checked = False)
chk_3D1.Checked = (chk_AutoTX1.Checked = False And chk_3D1.Checked = True)


So that you cover the 'directionality' of which is checked first?

Though the right way would be to put in a handler for each checkbox that unchecks the other

chk_3d1.CheckChanged(blah blah)
chk_AutoTK.Checked = !chk3d1.Checked

chk_AutoTX.CheckChanged(blah blah)
chk_3D1.Checked = !chkAutoTX.Checked



But let me ask you... What is the goal of all this? We had a completely functioning mechanism. What is the point of what you're doing?

This post has been edited by tlhIn`toq: 18 June 2012 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#28 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 18 June 2012 - 06:01 PM

Your right, the mechanism we arrived at functions perfectly, however I need additional functionality. I am stepping through adding the different functionality so as to not do too much at once. The code I posted actually results in the right values, it just doesnt uncheck the boxes. Which could be confusing to users.

The program is a set up utility for an unmanned aerial vehicle. There are many checkboxes that enable or disable certain functions.

The AutoTx1 and 3D1 in my examples enable Auto Leveling, and 3D "stunt" mode respectively. These two functions cannot be enabled at the same time, and the flight controller (hardware) does not handle this. So I need to do it in the set up utility.

There are 8 checkboxes for each function which correspond to channels and switch positions on the radio transmitter that controls the UAV. The values for these functions must be sent to the hardware as integers. So I have to create integers (as we accomplished) out of the 8 checkboxes.

There are a total of 9 different functions that use a set of 8 checkboxes each. However, there are only 4 that are conditional and cannot be used together.

This is actually a re-make of an existing application that is written poorly, hard to understand and doesnt function well. It was written by the Chinese hardware developers in what appears to be VB 98.

I am recreating it as an open source project for the community.

I have attached the original program if you want to see how their checkbox functionality works. In the upper right "custom area". However, the way it is written, it will try to send serial data on EVERY EVENT, and if a port isnt opened it will bark at you every time. This is one of the things I needed to improve.

I can upload my code too if you want to have a look.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#29 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Integer to 8 checkboxes checked

Posted 18 June 2012 - 06:11 PM

Quote

The code I posted actually results in the right values, it just doesnt uncheck the boxes. Which could be confusing to users.


The you should re-check, re-run or re-interpret the C# code I provided as it DOES uncheck all the boxes when the numberUpDown value changes.

The way you are going about this is far more work than needed.
Was This Post Helpful? 0
  • +
  • -

#30 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 18 June 2012 - 06:31 PM

View PosttlhIn`toq, on 18 June 2012 - 06:11 PM, said:

The way you are going about this is far more work than needed.


Yes, I know, but Im not a professional programmer nor used to this type of programming, so I have to do it how I can even if its not the "best" way, as long as it works properly.
Was This Post Helpful? 0
  • +
  • -

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