TextBox TextChanged错误

时间:2013-07-02 13:03:18

标签: vb.net winforms textbox labels

我的代码需要一点工作

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

我的文本框仅适用于一位数字。 所以我的错误是当我在文本框中输入两位数字时,它实际上更新了我的标签,但是当我按退格键时它不再更新。

2 个答案:

答案 0 :(得分:1)

变量TotalPrice的值随着每个新输入(无论是大于还是小于前一个)而增长,从而增加lblPrice.Text的值。例如:

txtBread.Text    TotalPrice     
   1                  1
   15                 16
   1                  17

如果您解释想要完成的内容,我可以更新您的代码。

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

答案 1 :(得分:0)

试试我的样品..

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