none
Giving Checkboxes Values RRS feed

  • Question

  • Hey,

    So I am currently coding my program and have encountered a problem, this problem is how to set a checkbox to have a value of 1 when checked.

    The A's are declared first:

    Dim A1, A2, A3, A4, A5 As Integer

    The if statements:

           If cbQ1A1.Checked = True Then
                A1 = cbQ1A1.Checked = 1
            Else
                A1 = 0
            End If
            If cbQ2A1.Checked = True Then
                A2 = cbQ2A1.Checked = 1
            Else
                A2 = 0
            End If
            If cbQ3A1.Checked = True Then
                A3 = cbQ3A1.Checked = 1
            Else
                A3 = 0
            End If
            If cbQ4A1.Checked = True Then
                A3 = cbQ3A1.Checked = 1
            Else
                A3 = 0
            End If
            If cbQ5A1.Checked = True Then
                A3 = cbQ3A1.Checked = 1
            Else
                A3 = 0
            End If

    ======================

    So what I am trying to do with this is then add it all up:

    Total = A1 + A2 + A3 + A4 + A5

    =======================

    After this I will then have a label which will display the total number.

    Many thanks for the help in advance :)

    Wednesday, November 1, 2017 5:34 PM

All replies

  • Hello,

    Please add to the first line of the form

    Option Strict On

    Note this will shows a ton of exceptions e.g. Checked property of a Checkbox is not an integer but of type Boolean. You could use .Tag property of the Checkbox controls to store data which when needed use CInt(someCheckBox.Tag) to get the tag's value.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, November 1, 2017 5:42 PM
    Moderator
  • Try this:

    A1 = If(cbQ1A1.Checked, 1, 0)
    A2 = If(cbQ2A1.Checked, 1, 0)
    A3 = If(cbQ3A1.Checked, 1, 0)
    . . .

    • Proposed as answer by Frank L. Smith Wednesday, November 1, 2017 7:40 PM
    Wednesday, November 1, 2017 5:46 PM
  • You can use each Checkbox's own sub routine to do that.

    I don't know what I.E. has to do with that though...

    Dim A1, A2, A3, A4, A5 As Integer
    
    Private Sub cbQ1A1._CheckedChanged(sender as Object, e as EventArgs) Handles cbQ1A1..CheckedChanged
       If cbQ1A1.Checked then A1 = 1 else A1 = 0
       Label1.Text = Cstr(A1 + A2 + A3 + A4 + A5)
    End Sub
    
    
    Private Sub cbQ2A1_CheckedChanged(sender as Object, e as EventArgs) Handles cbQ2A1.CheckedChanged
       If cbQ2A1.Checked then A2 = 1 else A2 = 0
       Label1.Text = Cstr(A1 + A2 + A3 + A4 + A5)
    End Sub
    
    ....and so on I suppose.
    
    


    La vida loca

    Wednesday, November 1, 2017 5:53 PM
  • Kermit,

    It looks like you want a count of all checkboxes which are checked.

    Here's another way you can do this: Set up a List(Of Checkboxes) and add each CheckBox of interest into that list, then use a bit of LINQ:

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim cbList As New List(Of CheckBox) _
                From {CheckBox1, CheckBox2, CheckBox3}
    
            Dim value As Integer = (From cb As CheckBox In cbList _
                                    Where cb.Checked).Count
    
    
            Stop
    
        End Sub
    End Class

    The variable "value" will have the number of checkboxes in the list which are checked.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, November 1, 2017 8:00 PM
  • Hi Kermit005,

    I agree with Frank L. Smith. It seems that you want to count the number of checked CheckBox.
    Here is a code, simple and easy to read.
    Dim checkedTotal As Integer = 0
    ' ---
    If (cbQ1A1.Checked) Then
    	checkedTotal += 1
    End If
    If (cbQ2A1.Checked) Then
    	checkedTotal += 1
    End If
    If (cbQ3A1.Checked) Then
    	checkedTotal += 1
    End If
    ' --- repeat like the above ---
    
    Regards,

    Ashidacchi

    Wednesday, November 1, 2017 10:09 PM
  • Hi Kermit005,

    You can use Convert.ToInt32() to convert Checkbox.CheckState into integer, like this:

     Dim a As Integer = Convert.ToInt32(CheckBox1.CheckState)
            Dim b As Integer = Convert.ToInt32(CheckBox2.CheckState)
            Dim c As Integer = Convert.ToInt32(CheckBox3.CheckState)
            Dim total = a + b + c

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 2, 2017 2:43 AM
    Moderator