1

我刚开始我的 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

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

4

3 回答 3

2

在进行比较之前,首先将您的 String 输入从 Textbox 转换为 Integer(并将其存储到 Integer 变量)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

其余的你自己做,否则你作为学生就没有更多的东西要学了。

于 2013-06-11T03:55:35.163 回答
0

代替

  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
于 2013-06-11T04:07:52.457 回答
0

你将需要这个............

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
于 2013-06-11T04:21:09.833 回答