为什么VB中的代码不起作用?

时间:2014-01-22 01:52:10

标签: vb.net windows visual-studio-2010 isnumeric

因此,对于该计划,任务是为汽车租赁公司创建一个程序。通过单选按钮选择不同种类的汽车,并且租用的汽车(每天)的成本乘以它将被租用的天数。我已经通过书籍完成了所有事情,所以出了什么问题?它不断提出我编码的消息框,告诉我我输入的数据不是数字(它是)

    Dim decJeepWrangler As Decimal = 55D
    Dim decLandRover As Decimal = 125D
    Dim decPickup As Decimal = 85D

    Dim intDays As Integer
    Dim decTotalCost As Decimal
    Dim decCost As Decimal

    If IsNumeric(txtDays) Then
        intDays = Convert.ToInt32(txtDays)

        If intDays > 0 Then
            If radJeepWrangler.Checked Then
                decCost = decJeepWrangler
            ElseIf radLandRover.Checked Then
                decCost = decLandRover
            ElseIf radPickup.Checked Then
                decCost = decPickup
            End If
            decTotalCost = intDays * decCost
            lblTotalCost.Text = decTotalCost.ToString("C")
        Else
            MsgBox("You entered " & intDays.ToString() & ". Enter a positive number", , "Input Error")
            txtDays.Text = ""
            txtDays.Focus()
        End If
    Else
        MsgBox("Enter how many days you will be renting", , "Input Error")
        txtDays.Text = ""
        txtDays.Focus()
    End If
End Sub

4 个答案:

答案 0 :(得分:4)

我没有足够的声誉点来评论,所以我不得不将其添加为答案,但看起来您使用txtDays代替txtDays.Text来启动。

答案 1 :(得分:1)

这是你要做的......

1) Add a TextBox control and name it txtDays.
2) Add a button.
3) Add the code shown below under the button_click event.

    Dim decJeepWrangler As Decimal = 55D
    Dim decLandRover As Decimal = 125D
    Dim decPickup As Decimal = 85D

    Dim intDays As Integer
    Dim decTotalCost As Decimal
    Dim decCost As Decimal

    If IsNumeric(txtDays.Text) Then
        intDays = Convert.ToInt32(txtDays.Text)

        If intDays > 0 Then
            If radJeepWrangler.Checked Then
                decCost = decJeepWrangler
            ElseIf radLandRover.Checked Then
                decCost = decLandRover
            ElseIf radPickup.Checked Then
                decCost = decPickup
            End If
            decTotalCost = intDays * decCost
            lblTotalCost.Text = decTotalCost.ToString("C")
        Else
            MsgBox("You entered " & intDays.ToString() & ". Enter a positive number", , "Input Error")
            txtDays.Text = ""
            txtDays.Focus()
        End If
    Else
        MsgBox("Enter how many days you will be renting", , "Input Error")
        txtDays.Text = ""
        txtDays.Focus()
    End If

答案 2 :(得分:0)

就像 Macoms01 一样,您需要使用 txtDays.Text 从TextBox中检索输入值。此外,在计算总成本之前,需要将decCost变量初始化为0以在OR处开始,编写IF语句以检查decCost = null并且如果是这样则设置为0.

最后,并非所有人都希望通过他们的建议获得报酬,所以不要听Neolisk。我在这里遇到过像他这样的人,他们只是想提供一个简单的答案。他们忘记了解如果有一个简单的答案,人们就不会在这里寻求帮助。

在任何情况下,如果我的回复对您有任何帮助,请在此处为其他人回答问题。

答案 3 :(得分:0)

Dim decJeepWrangler As Decimal = 55D
Dim decLandRover As Decimal = 125D
Dim decPickup As Decimal = 85D
Dim intDays As Integer = 0
Dim decTotalCost As Decimal = 0
Dim decCost As Decimal = 0
'
If Trim(txtDays.text) <> "" Then
    If cint(trim(txtDays.text)) > 0 Then
        intDays = CInt(txtDays.Text)
        If radJeepWrangler.Checked Then
            decCost = decJeepWrangler
        ElseIf radLandRover.Checked Then
            decCost = decLandRover
        ElseIf radPickup.Checked Then
            decCost = decPickup
        End if
        if decCost > 0 then
            decTotalCost = intDays * decCost
            lblTotalCost.Text = decTotalCost.ToString()
        Else
            msgbox("please select a vehicle", ,"Error")
            txtDays.Text = "0"
            txtDays.Focus()
        End If
    Else
        MsgBox("Enter how many days you will be renting", , "Error")
        txtDays.Text = "0"
        txtDays.Focus()
    End If
else
    MsgBox("Enter how many days you will be renting", , "Error")
    txtDays.Text = "0"
    txtDays.Focus()
end if

一些错误或拼写错误立刻跳出来,上面的代码应该可以正常工作,请试试看看。