Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to restrict a negative value in a textbox

Posted on 2011-09-27
5
Medium Priority
?
321 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month7 days, 8 hours left to enroll

824 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