Reputation: 75
My code needs a little work
Public Class Form1
Dim Bread, TotalPrice As Double
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
TotalPrice = TotalPrice - Bread
lblBread.Text = Bread.ToString
lblPrice.Text = TotalPrice.ToString
Else
Bread = Val(txtBread.Text) * 3.25
lblBread.Text = Bread.ToString
TotalPrice = TotalPrice + Bread
lblPrice.Text = TotalPrice.ToString
End If
End Sub
End Class
My textbox is good for a one-digit number only. So my error here is when i input two-digit numbers on my text box, it actually updates my labels, but when i press backspace it does not update anymore.
Upvotes: 0
Views: 590
Reputation: 5719
Try with my sample ..
Public Class Form1
Dim Bread As Double
Dim TotalPrice As Double = 100 '---> maybe this is a result from a function
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
Bread = 0
Else
Bread = Val(txtBread.Text) * 3.25
End If
lblBread.Text = Bread.ToString
lblPrice.Text = (TotalPrice + Bread).ToString
End Sub
End Class
Upvotes: 0
Reputation:
The value of the variable TotalPrice
grows with each new input (no matter if it is bigger or smaller than the previous one) and thus the value of lblPrice.Text
. For example:
txtBread.Text TotalPrice
1 1
15 16
1 17
If you explain what you want to accomplish exactly, I can update your code.
Dim Bread As Double
Dim TotalPrice as Double = 5 'Any constant value you want
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
lblBread.Text = Bread.ToString
lblPrice.Text = Convert.ToString(TotalPrice - Bread)
Else
Bread = Val(txtBread.Text) * 3.25
lblBread.Text = Bread.ToString
lblPrice.Text = Convert.ToString(TotalPrice + Bread)
End If
End Sub
Upvotes: 1