我的累加器和计算在VB 2010中不起作用

时间:2012-04-13 00:13:55

标签: vb.net

好的,所以我的累加器现在正在添加并在表单中正确显示。但是,我的if ... then语句出了问题,并且在达到125后它没有把它扔到第二级。我可以达到125,我需要输入一个条目来启用我的按钮。任何帮助表示赞赏!

* 编辑以显示使用Do While循环更新。现在导致输入错误msgbox ... *

的问题
do while decTotalCredits < 125 
        If IsNumeric(txtCredit.Text) Then
            ' This statement will convert the string entered to decimal and establish the 
            ' input as the decCredit Variable
            decCredit = Convert.ToDecimal(txtCredit.Text)
            ' This Case Statement is to verify that the correct denominations of coins are 
            ' being entered in the machine. 
            Select Case decCredit
                Case 5, 10, 25, 100
                    ' This line adds the newly entered credit to the 
                    ' exsisting total
                    decTotalCredits += decCredit
                    lblTotal.Text = Convert.ToString(decTotalCredits)
                    lblTotal.Visible = True
                    ' reset the text input box for the credit amount
                    txtCredit.Clear()
                    txtCredit.Focus()
                Case Else
                    ' This message will appear if a Credit is entered that does not
                    ' conform to normal coins
                    MsgBox("Please enter a valid coin amount", , "Invalid Amount Entered")
            End Select
        Else
            ' This message will occur when a user inputs a non-numeric entry
            MsgBox("Please enter a valid Coin amount", , "Input Error")
        End If
    Loop

    ' Loop should complete when credits hit 125 and activate this code
    ' Once the credits are reached the prompt to make selection is visible. 
    lblMakeSelection.Visible = True
    ' Once the credits are reached, the buttons for selection become enabled.
    btnDietPepsi.Enabled = True
    btnPepsi.Enabled = True
    btnSierraMist.Enabled = True
    btnLemonade.Enabled = True
    btnDrPepper.Enabled = True
    btnWater.Enabled = True

End Sub

1 个答案:

答案 0 :(得分:1)

从你给我们的代码中,它将进入循环,添加到总数,擦除txtCredit文本框,再次启动循环,然后显示错误消息框,因为txtCredit不再是数字。

假设逻辑位于按钮单击或TextBox Validate例程中,建议您删除循环并在启用按钮之前添加“If decTotalCredit&gt; = 125 Then”语句。