vb.net VB 防止负整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17036093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:54:47  来源:igfitidea点击:

VB Prevent negative integers

vb.netif-statement

提问by Fractal Fear

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:

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

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

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.

我知道一些 else 和 if 语句是多余的,但是如果你能给我一些关于压缩和清理的简单方法的指示,我将不胜感激。

采纳答案by Santosh

Instead of

代替

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

Use this

用这个

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

回答by Edper

Convert first your String input from Textbox into Integer (and store it to an Integer variable) using TryParsebefore comparing like:

TryParse在比较之前,首先将您的字符串输入从文本框转换为整数(并将其存储到整数变量):

    Dim valueA, valueB, valueC As Integer
    Dim intPackA, intPackB, intPackC, intCalc As Integer

    Int32.TryParse(txtPackA.Text, valueA)
    Int32.TryParse(txtPackB.Text, valueB)
    Int32.TryParse(txtPackC.Text, valueC)

    If valueA <=0 Or valueB <= 0 Or valueC <= 0 Then
        lblCalc.Text = "Please enter positive numeric values"
        Exit Sub
    End If

Do the rest youserlf or else you have no more to learn as a student.

剩下的你自己做,否则你作为学生就没有更多的东西可以学习了。

回答by matzone

You will need this ........

你会需要这个........

Private Sub TB_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPackA.KeyPress, txtPackB.KeyPress, txtPackC.KeyPress

    Dim sN As String = "0123456789"
    Dim sO As String = Chr(8) & Chr(13) & Chr(1) & Chr(3) & Chr(22)        

    If Not (sN & sO).Contains(e.KeyChar) Then

        e.Handled = True   

    End If

End Sub


Private Sub TB_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPackA.TextChanged, txtPackB.TextChanged, txtPackC.TextChanged

     lblCalc.Text = format(val(txtPackA.Text) + val(txtPackB.Text) + val(txtPackC.Text))

End Sub