Home > DeveloperSection > Forums > VB Prevent negative integers
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    June-11-2013 6:57 AM

 VB.Net VB.Net 
Ratings:


 1 Reply(s)
 873  View(s)
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. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-12-2013 8:05 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick