Solved

How to restrict a negative value in a textbox

Posted on 2011-09-27
5
313 Views
Last Modified: 2012-05-12
How can I only allow positive values in my textbox? I want the user to only enter positive numbers. How can I flag them?
0
Comment
Question by:cmdolcet
5 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36709205
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36709233
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
 

Author Comment

by:cmdolcet
ID: 36709244
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36709253
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 125 total points
ID: 36709280
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 49
Allow user to hide and show datagridview columns 4 25
Gridview selected row 9 44
Sending receiving text messages in vb.net 15 33
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question