Want to verify that textbox contains numeric data only

Posted on 2007-10-03
Last Modified: 2012-05-05
I have two issues going on here.  I want to verify that data in the textbox is only numeric, no spaces or letters.  It also can not be a negative entry either.  I also run into an issue that if an entry is found to be a duplicate of one that is already in the array, it gets put in anyway.  Below is the code I have so far (minus the form designer created code):

Public Class frmEnterNum
    Inherits System.Windows.Forms.Form
    Private Sub frmEnterNum_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        variables.CurrentIndex = -1
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim Found As Boolean
        Dim number As Integer
        If txtNum.Text = "" Then        'check if text box is empty
            lblMessage.Text = "Enter a number that is between 1 and 61."
            txtNum.Text = ""
        End If
        number = Val(txtNum.Text)
        If CurrentIndex = 5 Then            'array is full
            MsgBox("All the numbers have been entered.")
        Else                                'array not full
            Found = IsDupe(number, Numbers) 'call the IsDupe function
            If Found Then
                lblMessage.Text = "This number already exists."
                txtNum.Text = ""
                'CurrentIndex = CurrentIndex - 1
                CurrentIndex = CurrentIndex + 1 'move to next free subscript in array and store the number in it
                Numbers(CurrentIndex) = number
                txtNum.Text = ""
            End If

        End If

    End Sub

    Public Function IsDupe(ByVal wantedNumber As Integer, ByVal numArray() As Integer) As Boolean
        Dim Index As Integer
        Dim Found As Boolean
        Index = 0
        Found = False 'searching hasn't started yet so Found should be false
        Do While (Found = False) And (Index <= CurrentIndex)
            'one repitition of the loop processes one number in the array
            If numArray(Index) = wantedNumber Then
                Found = True
            Else 'Current subscript does not have number being searched for
                Index = Index + 1 'so go to the next subscript in the array
            End If
        If Found Then   'i.e. if Found = true
            Return True
            Return False
        End If

    End Function

    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
    End Sub
End Class
Question by:emtshea
    LVL 48

    Accepted Solution

    Didn't understand to well your code but you can check if it is only number and not negative this way

    If IsNumeric(txtNum.Text) Then
      If txtNum.Text > 0 Then
           ' OK
      End IF
    End If

    LVL 2

    Expert Comment

    I needed to do exactly the same thing so I created a function that I call after catching  textbox.keydown
    I pass the keypress to the function which returns an integer. If the result is -1 then I ignore the keypressed. Function as follows as well as an exaple that calls it.

     Public Function IsNumberKey(ByVal KCode As Keys) As Integer
            Dim i As Integer
            Select Case KCode
                Case Keys.NumPad0
                    i = 0
                Case Keys.NumPad1
                    i = 1
                Case Keys.NumPad2
                    i = 2
                Case Keys.NumPad3
                    i = 3
                Case Keys.NumPad4
                    i = 4
                Case Keys.NumPad5
                    i = 5
                Case Keys.NumPad6
                    i = 6
                Case Keys.NumPad7
                    i = 7
                Case Keys.NumPad8
                    i = 8
                Case Keys.NumPad9
                    i = 9
                Case Keys.D0
                    i = 0
                Case Keys.D1
                    i = 1
                Case Keys.D2
                    i = 2
                Case Keys.D3
                    i = 3
                Case Keys.D4
                    i = 4
                Case Keys.D5
                    i = 5
                Case Keys.D6
                    i = 6
                Case Keys.D7
                    i = 7
                Case Keys.D8
                    i = 8
                Case Keys.D9
                    i = 9

                Case Else
                    i = -1
            End Select
            IsNumberKey = i
        End Function

    Private Sub CaptureQty(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbQty.KeyDown
            Dim k As String
            Dim kint As Integer
            Dim IsANum As Boolean
            Dim kc As Keys

            k = e.KeyValue.ToString
            kc = e.KeyCode
            kint = Login.IsNumberKey(kc)

            IsANum = kint <> -1

            If Not IsANum Then

                Select Case kc
                    Case Keys.Enter
                        e.Handled = True

                    Case Keys.Back
                    Case Keys.Delete
                    Case Keys.Right
                    Case Keys.Left
                    Case Else
                        e.SuppressKeyPress = True
                End Select
            End If
        End Sub

    Hope this does the trick!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    18 Experts available now in Live!

    Get 1:1 Help Now