none
code for telling if textbox contains an integer or not

    Question

  • Basicall, all i need is some simple code that will check if the value in a textbox is a positive integer or not. Something like this:

    If textbox1.text is a positive integer then execute code

    Of course that syntax is totally incorrect, but that's the general idea of what i want to be able to do. Any ideas?

    -MiniatureBeast

    Sunday, July 10, 2011 5:32 AM

Answers

  • Why not something as simple as this:

    Dim intValue As Integer
        If Integer.TryParse(TextBox1.Text, intValue) AndAlso _
        intValue > 0 Then
          MessageBox.Show("The value you entered is positive integer", "My Integer Value Test")
        ElseIf Not Integer.TryParse(TextBox1.Text, intValue) Or intValue < 0 Then
    
          MessageBox.Show("Please enter only positive integers", "Data entry error")
    
        End If
    

     


    Only performance counts!
    • Proposed as answer by Shaoun1000 Sunday, July 10, 2011 10:37 AM
    • Marked as answer by MiniatureBeast Sunday, July 10, 2011 4:07 PM
    Sunday, July 10, 2011 9:39 AM

All replies

  • ideas? Sure

    you need to validate whether a value entered into a TextBox is:

       1. an integer

       2. non-negative

    use Integer.TryParse to test whether it is an Integer or not, and then test if that Integer is > 0

    so, in a button click event:

    create an Integer variable

    use an If...Then block to test if it is an Integer AndAlso if it is > 0

    if it passes both those validations, do something. If not do something else, such as show a message saying the entered value must be an integer greater then zero

     

    • Proposed as answer by Blackwood Sunday, July 10, 2011 11:01 AM
    Sunday, July 10, 2011 5:43 AM
  • hai,

    If you want only positive integers to be entered into ur textbox, then on the key press event of ur textbox write the code below..

            Dim allowed As String = "0123456789"

            Dim curchar As Integer = Asc(e.KeyChar)

            If (allowed.IndexOf(e.KeyChar) = -1) And (curchar <> 8) Then

                e.Handled = True

            End If

    This will only allow positive integers in your text box

     

    Hope it will help

     

    Sunday, July 10, 2011 5:50 AM
  • I'm not sure but I think this will do it too.

    if not textbox1.contains("-") then
    'execute...
    end if
    

     
    Sunday, July 10, 2011 7:41 AM
  • Why not something as simple as this:

    Dim intValue As Integer
        If Integer.TryParse(TextBox1.Text, intValue) AndAlso _
        intValue > 0 Then
          MessageBox.Show("The value you entered is positive integer", "My Integer Value Test")
        ElseIf Not Integer.TryParse(TextBox1.Text, intValue) Or intValue < 0 Then
    
          MessageBox.Show("Please enter only positive integers", "Data entry error")
    
        End If
    

     


    Only performance counts!
    • Proposed as answer by Shaoun1000 Sunday, July 10, 2011 10:37 AM
    • Marked as answer by MiniatureBeast Sunday, July 10, 2011 4:07 PM
    Sunday, July 10, 2011 9:39 AM
  • hai,

    If you want only positive integers to be entered into ur textbox, then on the key press event of ur textbox write the code below..

     

            Dim allowed As String = "0123456789"

            Dim curchar As Integer = Asc(e.KeyChar)

            If (allowed.IndexOf(e.KeyChar) = -1) And (curchar <> 8) Then

                e.Handled = True

            End If

     

    This will only allow positive integers in your text box

     

    Hope it will help

     

    What happens if the user pastes into the textbox?  Does KeyPress happen?

    Serial Port      Random      Microsoft® Community Contributor 2011
    Sunday, July 10, 2011 11:36 AM
  •  

    Thank you dbasnett,

    I didnt think about that, okay this is the modification,

    first set the shortcut enabled property of the textbox to false (disables context strip)

    Then on the keydown event

     

     

            If e.Modifiers = Keys.Control Then

                e.Handled = True

                TextBox2.SelectionLength = 0

                TextBox2.ReadOnly = True

            Else

                TextBox2.ReadOnly = False

            End If

    This will prevent the copy paste.

     

    Sunday, July 10, 2011 12:35 PM
  • wow! Sylva's code uses the exact same name for the Integer variable I had, and the rest of the code is almost exactly the same. He must be hacking into my computer and stealing my code :)

    the only difference is using Else instead of ElseIf - you only have 2 conditions: the entered value is valid or not. So if the validation fails, no need to test for it again 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intValue As Integer
        If Integer.TryParse(TextBox1.Text, intValue) AndAlso intValue > 0 Then
          MessageBox.Show("you have entered a positive integer")
        Else
          MessageBox.Show("you have entered either a negative number or not an integer")
        End If
      End Sub
    

     

    • Proposed as answer by Blackwood Sunday, July 10, 2011 4:26 PM
    Sunday, July 10, 2011 2:05 PM
  • My $.02

     

    Public Class Form1

        Dim myNum As Integer? = Nothing

        Private Sub TextBox1_TextChanged(sender As System.Object, _
                                         e As System.EventArgs) Handles TextBox1.TextChanged
            Dim s As String = TextBox1.Text.Replace(",", "") 'get rid of comma's
            Dim foo As Integer
            'is number
            If Integer.TryParse(s, foo) Then
                myNum = foo 'yes
            Else
                myNum = Nothing 'no
            End If
            Label1.Text = myNum.ToString
        End Sub

        Private Sub Button1_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button1.Click
            If myNum.HasValue AndAlso myNum > 0 Then
                Label2.Text = "Good"
            Else
                Label2.Text = "Bad"
            End If
        End Sub
    End Class


    Serial Port      Random      Microsoft® Community Contributor 2011
    Sunday, July 10, 2011 2:43 PM
  • Wow, thank you all for so much contribution. These are all great bits of code. I knew how i was going to go about doing it, but i diidn't really know the correct syntax and you've all really helped! Thanks guys  :)
    Sunday, July 10, 2011 4:07 PM
  • I realize this isn't what you meant (or at least I don't think it is, having read through this), but what your question was "if the textbox CONTAINS" an integer and if so, is the integer positive.

    Whether or not it IS a valid integer wasn't the question stated; it was whether or not it contains one somewhere in the textbox. So if they were to enter, for example, "TG57y", the answer should be yes - it CONTAINS a valid integer. You could use Val but I hate that! I wish it were removed, honestly. It confuses people.

    At any rate, just in case someone else stubles on this thread and actually wants to do that (that is, see if the textbox contains a number and, if so, if the number is positive), I thought I'd toss my two cents in:

     


      Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    
        Dim containsValidNumbers As Boolean = False
        Dim numberPortion As Integer = 0
        Dim numberBuilder As String = ""
    
        For Each c As Char In TextBox1.Text
          If IsNumeric(c) Then
            numberBuilder = numberBuilder & c
          Else
            If c = "-"c Then
              numberBuilder = numberBuilder & c
            ElseIf c = "."c Then
              numberBuilder = numberBuilder & c
            End If
          End If
        Next
    
        If Not numberBuilder Is String.Empty Then
          If Integer.TryParse(numberBuilder, numberPortion) Then
            If numberPortion > 0 Then
              containsValidNumbers = True
            Else
              containsValidNumbers = False
            End If
          Else
            containsValidNumbers = False
          End If
        End If
    
        If containsValidNumbers Then
          ' The string in the textbox does in fact contain
          ' (or possibly completely is) a positive integer.
          '
          ' Note that if they enter a negative sign (-) or
          ' a decimal (.), this is recorded and assumes they
          ' mean the number to be negative and/or to be a
          ' type double in which case, the result of the
          ' variable "containsValidNumbers" will be false.
          Stop
        Else
          ' The string in the textbox either has no numbers,
          ' or the number didn't pass the test of being a
          ' positive integer
          Stop
        End If
    
      End Sub
    

     

    Sunday, July 10, 2011 4:31 PM