forum

Home / DeveloperSection / Forums / VB Prevent negative integers

VB Prevent negative integers

marcel ethan 1776 11-Jun-2013
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. 

Updated on 12-Jun-2013

Can you answer this question?


Answer

1 Answers

Liked By