Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Want to verify that textbox contains numeric data only

Posted on 2007-10-03
2
Medium Priority
?
1,622 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
0
Comment
Question by:emtshea
2 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 20009983
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

0
 
LVL 2

Expert Comment

by:RayAtVittoria
ID: 20010036
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
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

581 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