We help IT Professionals succeed at work.

Want to verify that textbox contains numeric data only

emtshea
emtshea asked
on
1,645 Views
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 = ""
            txtNum.Focus()
        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 = ""
                txtNum.Focus()
                'CurrentIndex = CurrentIndex - 1
            Else
                CurrentIndex = CurrentIndex + 1 'move to next free subscript in array and store the number in it
                Numbers(CurrentIndex) = number
                txtNum.Text = ""
                txtNum.Focus()
            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
        Loop
        If Found Then   'i.e. if Found = true
            Return True
        Else
            Return False
        End If

    End Function

    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        Me.Close()
    End Sub
End Class
Comment
Watch Question

IT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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
                    PostChange()

                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!
Cheers

Ray

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.