Problem adding values assigned to check boxes

Hekzdaddy 121 Reputation points
2021-02-25T00:13:50.58+00:00

Hi all, I am running a simple program but am stuck when trying to tally up my check boxes. I assigned my chkBoxes values as such:

Const CdblchkLevel3 As Integer = 5 Const CdblchkCloud As Decimal = 10 Const CdblchkOnSite As Decimal = 15 When I try and add them all up as :
If chkOnSite.Checked AndAlso chkCloud.Checked AndAlso chkLevel3.Checked Then lblFeatures.Text = CdblchkLevel3 + CdblchkOnSite + CdblchkCloud
End If

I get 15 as the answer.

Additionally, My total wont show up as currency, this is what I used to display: lblFeatures.Text = ToString("c") When that feature is on, I get "0" as the answer.

Any tips/suggestions would be helpful

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,660 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2021-02-25T02:16:09.033+00:00

    If it works then that is up to you to decide. But I would not use formatted values or have a string if IF statements. Below is a adaptation of my first reply which does not care or know about how many CheckBox controls are used. This is how I would approach this with what you asked for,

    Imports System.ComponentModel
    
    Public Class Form1
        Private CheckBoxes As New List(Of CheckBoxCustom)
    
        Private Sub AllCheckBoxs_CheckedChanged(sender As Object, e As EventArgs)
    
            Label1.Text = CheckBoxes.
                Where(Function(cb) cb.Checked).
                Select(Function(item) item.DecimalValue).
                Sum().
                ToString("C2")
    
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
            CheckBoxes = Controls.OfType(Of CheckBoxCustom).ToList()
    
            For Each checkBoxCustom As CheckBoxCustom In CheckBoxes
                AddHandler checkBoxCustom.CheckedChanged, AddressOf AllCheckBoxs_CheckedChanged
            Next
    
        End Sub
    End Class
    Public Class CheckBoxCustom
        Inherits CheckBox
        <Category("Behavior"), Description("Decimal value")>
        Public Property DecimalValue() As Decimal
    End Class
    
    1 person found this answer helpful.

  2. Karen Payne MVP 35,386 Reputation points
    2021-02-25T01:23:38.933+00:00

    Hello,

    Try the following which uses a custom CheckBox with a property of DecimalValue which guarantees a value. If alternate is to set the Tag property of each regular CheckBox then you need to cast the .Tag property via CDec to then get the sum.

    The custom CheckBox should be in it's own class file.

    Imports System.ComponentModel  
      
    Public Class Form1  
        Private CheckBoxes As New List(Of CheckBoxCustom)  
      
        Private Sub CheckBoxCustom1_CheckedChanged(sender As Object, e As EventArgs) _  
            Handles CheckBoxCustom1.CheckedChanged, CheckBoxCustom2.CheckedChanged, CheckBoxCustom3.CheckedChanged  
      
            Label1.Text = CheckBoxes.  
                Where(Function(cb) cb.Checked).  
                Select(Function(item) item.DecimalValue).  
                Sum().  
                ToString("C2")  
      
        End Sub  
      
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            CheckBoxes = Controls.OfType(Of CheckBoxCustom).ToList()  
        End Sub  
    End Class  
    Public Class CheckBoxCustom  
        Inherits CheckBox  
        <Category("Behavior"), Description("Decimal value")>  
        Public Property DecimalValue() As Decimal  
    End Class  
      
    

    71828-11111111111.png

    71873-3333333333333.png

    0 comments No comments

  3. Hekzdaddy 121 Reputation points
    2021-02-25T01:51:41.39+00:00

    I ended up doing this, is this correct also? seems to display properly, do you see any issues with this code?

    'Check for additional services
    If chkLevel3.Checked = True Then
    lblFeatures.Text = "$5.00"
    End If
    If chkOnSite.Checked Then
    lblFeatures.Text = "$10.00"
    End If
    If chkCloud.Checked = True Then
    lblFeatures.Text = "$15.00"
    End If

            If chkOnSite.Checked AndAlso chkCloud.Checked AndAlso chkLevel3.Checked Then
                lblFeatures.Text = "$30.00"
            ElseIf chkOnSite.Checked And chkCloud.Checked Then
                '
                lblFeatures.Text = "$25.00"
            ElseIf chkOnSite.Checked And chkLevel3.Checked Then
    
                lblFeatures.Text = "$15.00"
            ElseIf chkLevel3.Checked And chkCloud.Checked Then
                lblFeatures.Text = "$20.00"
            End If
    
    0 comments No comments