VB PREVENT NEGATIVE INTEGERS

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
VB.Net 
 1085  View(s)
Ratings:
Rate this:
Hi Expert,

How to calculate 3 text boxes  value (only positive value) and display into single label . If user interned negative value it display error message. 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. 

My line of code as following

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

Please guide me, where i'm wrong?

Any help on above is really appreciated. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: VB Prevent negative integers

    Hi Marcel,


    In place of following line of code

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

    Use this line of code

    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

    I hope it helpful for you.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!