How to restrict a negative value in a textbox

How can I only allow positive values in my textbox? I want the user to only enter positive numbers. How can I flag them?
cmdolcetAsked:
Who is Participating?
 
Kiran SonawaneConnect With a Mentor Project LeadCommented:
Try this

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim allowedChars As String = "0123456789."

    If allowedChars.IndexOf(e.KeyChar) = -1 Then
        ' Invalid Character
        e.Handled = True
    End If

End Sub
0
 
Luis PérezSoftware Architect in .NetCommented:
Put this code in the KeyPress event for the text box:

        If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If


Hope that helps.
0
 
Kiran SonawaneProject LeadCommented:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

' textbox1 just accept numeric data

If Char.IsNumber(e.KeyChar) Then

e.Handled = False

Else

MessageBox.Show("plz click numbers only")

e.Handled = True

End If

End Sub
0
 
cmdolcetAuthor Commented:
OK that works for any negative number but when I try to enter a 0.6 it will not allow my to enter the .
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
All Courses

From novice to tech pro — start learning today.