Solved

How to restrict a negative value in a textbox

Posted on 2011-09-27
5
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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