Results 1 to 4 of 4

Thread: Detect or suppress invalid characters in TextBox

  1. #1

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Detect or suppress invalid characters in TextBox

    Hey there,

    I have a TextBox with a list of filenames in it. The user is able to change these file names, but I don't want them to enter invalid characters, such as slashes or question marks.

    I've tried two approaches, but they are not satisfactory:

    1. Detect the key pressed with the KeyDown event:
    Use e.KeyValue to detect keys like Keys.Oem5 (= backslash), then use 'e.SuppressKeyPress = True' to suppress the key.
    Problem is, with different Keyboard layouts, The Oem5-key isn't always a backslash.

    2. Filter the text:
    Use the TextChanged event and do a Replace("\", "") on the text for every invalid character (there's 9 of them).
    Of course this will create 'lag' with every keystroke, especially if there's a lot of text. Also the text-cursor and scrollbar will jump back to the top-left when using Replace.

    In short: I'm looking for a way to detect the character that is being entered and suppress or remove it when invalid.

    Any help is appreciated.

    Best regards,
    Alexander.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Detect or suppress invalid characters in TextBox

    I do not know if this will be the perfect solution. This one prevents the user from typing backslash, star or character zero

    vb.net Code:
    1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    2.     TextBox1.Text = TextBox1.Text.Trim({"\"c, "*"c, Chr(0)})
    3.     TextBox1.Select(TextBox1.Text.Length, 0)
    4. End Sub

    on a side note, this one will only allow alpha characters with space or backspace or delete. Technically there is a way to insert special characters but not directly with a single keyboard key. If you want to prevent those also include them in the string.

    vb.net Code:
    1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    2.     TextBox1.Text = TextBox1.Text.Trim("`1234567890-=~!@#$%^&*()_+[]\{}|;':"",./<>?".ToCharArray)
    3.     TextBox1.Select(TextBox1.Text.Length, 0)
    4. End Sub
    Last edited by kaliman79912; Jan 5th, 2012 at 05:31 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Detect or suppress invalid characters in TextBox

    You can check against these two arrays

    IO.Path.GetInvalidFileNameChars
    IO.Path.GetInvalidPathChars

  4. #4

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: Detect or suppress invalid characters in TextBox

    @kaliman79912 That will only work if the invalid character is either the first or the last character. People will type in the middle of file names as well.

    @kevinstructor Thanks for that, it may prove useful later!
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

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