Solved

How to restrict a negative value in a textbox

Posted on 2011-09-27
5
311 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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now