שתף באמצעות


How do I add values(Prices) to CheckBoxes, Radiobuttons and Perform Calculations?

Question

Tuesday, March 22, 2011 5:15 AM

Good evening everyone!

I am trying to put together a simple form with prices assigned to Radio buttons and check boxes.

I started by creating a simple form with four Windows Form control, two check boxes and two radio buttons, there is also a NumberUpDown Control for quantity but I have not added that yet.

Basically the user make selections, the price is calculated with total and subtotal and displayed on a label.

I am trying to start as basic as possible then work my way up to more complex calculations as I progress.

Here is my Screw up version: :)

Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  End Sub

  Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

  End Sub

  Private Sub lblDisplayFinalPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDisplayFinalPrice.Click

  End Sub

  Private Sub btnCaculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCaculate.Click

    Dim decGetAllCheckBoxPrice As Decimal
    Dim decaddTaxes As Decimal = 0.65
    Dim decCheckBox1Price As Decimal
    Dim decCheckBox2Price As Decimal
    Dim dedRadioButton1Price As Decimal
    Dim decRadioButton2Price As Decimal
    Dim decSubTotalNoInterest As Decimal

    If CheckBox1.Checked = True Then
      decCheckBox1Price += 1
    End If

    If CheckBox2.Checked = True Then
      decCheckBox2Price += 1
    End If

    If RadioButton1.Checked = True Then
      dedRadioButton1Price += 1
    End If

    If RadioButton2.Checked = True Then
      decRadioButton2Price += 1
    End If

    decGetAllCheckBoxPrice = (decCheckBox1Price + decCheckBox2Price + dedRadioButton1Price + decRadioButton2Price = decSubTotalNoInterest)
    Dim displayPrice As String = Convert.ToString(decSubTotalNoInterest)
    lblDisPlaySubTotal.Text = displayPrice
  End Sub

  Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

  End Sub

  Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

  End Sub

  Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

  End Sub
End Class

 

I am not sure I am doing this right, I know this seem simple but this is very new to me.

Any directions is greatly appreciated.

 

BL

All replies (5)

Tuesday, March 22, 2011 8:52 AM ✅Answered | 1 vote

Hi

You can store the price of each check box or Radio button in its Tag property. Later on calculation convert the Tag value to Double for performing operations.

Regards, Anoop


Tuesday, March 22, 2011 10:40 AM ✅Answered

Hi Boiling,

Anoop's method should be working.

Alternatively approcah is that you can create your own checkbox and radio button control and let them have price property. The control contains the price value like control has a caption property too.

Below is the sample code of add price in checkbox

Public Class PriceCheckBox
  Inherits CheckBox
  Private _Price As Double
  Public Property Price() As Double
    Get
      Return _Price
    End Get
    Set(ByVal value As Double)
      _Price = value
    End Set
  End Property
  Sub New(ByVal value As Double)
    Me.Price = value
  End Sub
End Class

You can use it like below

If CheckBox1.Checked = True Then
   CheckBox1.price += 1
End If

You even can move all the condition "If CheckBox1.Checked" inside the checkbox, the code will be like below.

Then all of IF checkbox/RadioButton.checked rules can be removed.

Public Property Price() As Double
    Get
      If Me.Checked Then
        Return _Price+1
      End If 
    End Get
    Set(ByVal value As Double)
      _Price = value
    End Set
  End Property

Regards


Saturday, April 2, 2011 1:08 PM

Hi Boling,

 

Any update?

 

How about your program? If it works, you can share your solutions & experience here, it will be very beneficial for other community members who have similar questions. Thanks.

 

Best regards,

Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Tuesday, April 5, 2011 9:14 AM

Hi Boling,

 

Thanks for posting in the MSDN Forum.

 

Any update? I have marked Anoop and Minimum's reply as answer, if you think it provides no help, please unmark it.

 

Thank you for your understanding and support.

 

Best Regards,

Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Friday, May 5, 2017 11:47 AM

it is working but need some  little change .Thank you for sharing your concept.