changing tetxtboxes to doubles and ints to add up

weldos

Member
Joined
May 19, 2013
Messages
5
Programming Experience
1-3
Private Sub btnCalc_Click(sender As System.Object, e As System.EventArgs) Handles btnCalc.Click
         CalculateCost()
 End Sub
Private Sub CalculateCost()
          txtbCat1Cost.Text = txtbCat1Items.Text * txtbCat1Price.Text
         txtbCat2Cost.Text = txtbCat2Items.Text * txtbCat2Price.Text
         txtbCat3Cost.Text = txtbCat3Items.Text * txtbCat3Price.Text
         txtbCat4Cost.Text = txtbCat4Items.Text * txtbCat4Price.Text
          txtbTotalCost.Text = Val(txtbCat1Cost.Text) + Val(txtbCat2Cost.Text) + Val(txtbCat3Cost.Text) + Val(txtbCat4Cost.Text)
         txtbFinalCost.Text = Val(txtbTotalCost.Text)
   
 If chkbLoyalty.CheckState = CheckState.Checked Then
            txtbCat1Items.Text = txtbCat1Items.Text + 1
             txtbTotalCost.Text = txtbTotalCost.Text - 4.0
  End If
If chkbStaff.CheckState = CheckState.Checked Then
            txtbFinalCost.Text = txtbFinalCost.Text * 0.9
 ElseIf chkbStaff.CheckState = CheckState.Unchecked Then
            txtbFinalCost.Text = txtbFinalCost.Text
 End If
End Sub
Private Sub btnCat1Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat1Plus.Click
         txtbCat1Items.Text = txtbCat1Items.Text + 1
 End Sub
Private Sub btnCat1Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat1Minus.Click
If txtbCat1Items.Text > 0 Then
            txtbCat1Items.Text = txtbCat1Items.Text - 1
  ElseIf txtbCat1Items.Text < = 0 Then
            txtbCat1Items.Text = "0"
End If
End Sub
Private Sub btnCat2Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat2Plus.Click
         txtbCat2Items.Text = txtbCat2Items.Text + 1
 End Sub
Private Sub btnCat2Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat2Minus.Click
If txtbCat2Items.Text > 0 Then
            txtbCat2Items.Text = txtbCat2Items.Text - 1
  ElseIf txtbCat2Items.Text < = 0 Then
            txtbCat2Items.Text = "0"
End If
End Sub
Private Sub btnCat3Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat3Plus.Click
         txtbCat3Items.Text = txtbCat3Items.Text + 1
 End Sub
PrivateSub btnCat3Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat3Minus.Click
If txtbCat3Items.Text > 0 Then
            txtbCat3Items.Text = txtbCat3Items.Text - 1
  ElseIf txtbCat3Items.Text < = 0 Then
            txtbCat3Items.Text = "0"
End If
End Sub
Private Sub btnCat4Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat4Plus.Click
         txtbCat4Items.Text = txtbCat4Items.Text + 1
 End Sub
Private Sub btnCat4Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCat4Minus.Click
If txtbCat4Items.Text > 0 Then
            txtbCat4Items.Text = txtbCat4Items.Text - 1
  ElseIf txtbCat4Items.Text < = 0 Then
            txtbCat4Items.Text = "0"
End If
End Sub
End Class
 
Hi,

First of all turn Option Strict On and always keep it on from now on to help you detect and avoid conversion issues in any project you write in the future. Once done, if you need to convert a String value, such as a TextBox.Text property, to a Numeric Type then use the TryParse method of the Numeric Type that you need to convert to. Have a Look at these two examples:-

Double.TryParse Method (String, Double) (System)
Int32.TryParse Method (String, Int32) (System)

Hope that helps.

Cheers,

Ian
 
Back
Top