0

我的代码需要一些工作

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

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

4

2 回答 2

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
于 2013-07-02T13:12:03.933 回答
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
于 2013-07-02T14:17:13.823 回答