VB防止负整数 [英] VB Prevent negative integers

查看:107
本文介绍了VB防止负整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始上VB的第一堂课,我的老师在48小时内没有回复电子邮件,我的项目即将到期。我希望你们能帮助我解决这个问题。我想要做的是获得一个程序来计算3个文本框的总数,并将其显示在一个标签中。问题是,3个文本框中的输入不能是负数。我试图设置它,所以当输入负数时,它会显示一条消息,只输入正数。但是,每次运行程序时,它只计算带有负数的文本框为0,并完成其余的计算。这是我的代码:

I am just starting my first class on VB, and my teacher hasn't responded to email in 48 hours and my project is due soon. I am hoping you guys can help me solve this. What I am trying to do is get a program to calculate the total of 3 text boxes and display it in a single label. The catch is, the input in the 3 text boxes cannot be a negative number. I am trying to set it so when a negative number is input it displays a message to enter positive numbers only. However every time I run the program it just calculates the text box with a negative number as 0 and completes the rest of the calculations. Here is my code:

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    Dim intPackA As Integer
    Dim intPackB As Integer
    Dim intPackC As Integer
    Dim intCalc As Integer
    Try
        If txtPackA.Text Or txtPackB.Text Or txtPackC.Text <= 0 Then
            lblCalc.Text = "Please enter positive numeric values"
        End If
        If txtPackA.Text >= 0 Then
            intPackA = txtPackA.Text * 79.2
        Else
            lblCalc.Text = "Please enter positive numeric values"
        End If
        If txtPackB.Text >= 0 Then
            intPackB = txtPackB.Text * 119.4
        Else
            lblCalc.Text = "Please enter positive numeric values"
        End If

        If intPackC >= 0 Then
            intPackC = txtPackC.Text * 149.5
        Else
            lblCalc.Text = "Please enter positive numeric values"
        End If

        intCalc = intPackA + intPackB + intPackC

        lblCalc.Text = "Package A:  " & intPackA.ToString("c") + vbCrLf & "Package B:  " & intPackB.ToString("c") + vbCrLf & "Package C:  " & intPackC.ToString("c") + vbCrLf + vbCrLf & "Grand Total: " & intCalc.ToString("c")
    Catch
        lblCalc.Text = "Please enter positive numeric values"
    End Try

End Sub

我知道其他一些和if语句都是多余的,但是如果你能给我一些简单的方法来指出凝聚和清理我也很感激。

I know some of the else and if statements are redundant, however if you could give me some pointers on an easy way to condense and clean up as well I would appreciate it.

推荐答案

而不是

  If txtPackA.Text Or txtPackB.Text Or txtPackC.Text <= 0 Then
            lblCalc.Text = "Please enter positive numeric values"
   End If

使用此

If txtPackA.Text <= 0 Then
    lblCalc.Text = "Please enter positive numeric values"
    txtPackA.Focus()
    Exit Sub
ElseIf txtPackB.Text <= 0 Then
    lblCalc.Text = "Please enter positive numeric values"
    txtPackB.Focus()
    Exit Sub
ElseIf txtPackC.Text <= 0 Then
    lblCalc.Text = "Please enter positive numeric values"
    txtPackC.Focus()
    Exit Sub
End If

这篇关于VB防止负整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆