Results 1 to 3 of 3

Thread: [2005] Error on typing word

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    [2005] Error on typing word

    I need code which will give messagebox when user types letter in textbox, when he types number all is ok! Thanks!

  2. #2
    Junior Member
    Join Date
    Jul 2007
    Location
    Bergen, Norway
    Posts
    31

    Re: [2005] Error on typing word

    Try this, perhaps?
    Code:
    Private Sub TextBox1_TextChanged(sender as object, e as system.eventargs) _
    Handles TextBox1.TextChanged
      If Not IsNumeric(TextBox1.Text) and Textbox1.Text <> "" Then
        MsgBox("Non-numeric text entered!")
      End If
    End Sub
    Note: This does not stop non-numeric characters from being entered, it only displays a messagebox. If you want to delete any non-numeric characters as they are entered, try something like this (as a quick and dirty solution):

    Code:
    Private ValidText as string = ""
    Private Sub TextBox1_TextChanged(sender as object, e as system.eventargs) _
    Handles TextBox1.TextChanged
      If Not IsNumeric(TextBox1.Text) and Textbox1.Text <> "" Then
        MsgBox("Non-numeric text entered!")
        TextBox1.Text = ValidText
      Else
        ValidText = TextBox1.Text
      End If
    End Sub

    Alternatively, you can handle the KeyDown event to stop the text being entered into the textbox completely, but that won't stop the user copying+pasting invalid characters into the textbox, so then you'll have to catch that as well.
    Last edited by Naigewron; Mar 16th, 2009 at 10:22 AM.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Error on typing word

    Use a NumericUpDown control instead of a TextBox
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width