|
-
Jan 5th, 2012, 05:11 PM
#1
Thread Starter
Fanatic Member
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!
-
Jan 5th, 2012, 05:23 PM
#2
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:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = TextBox1.Text.Trim({"\"c, "*"c, Chr(0)})
TextBox1.Select(TextBox1.Text.Length, 0)
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:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = TextBox1.Text.Trim("`1234567890-=~!@#$%^&*()_+[]\{}|;':"",./<>?".ToCharArray)
TextBox1.Select(TextBox1.Text.Length, 0)
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
-
Jan 5th, 2012, 05:24 PM
#3
Re: Detect or suppress invalid characters in TextBox
You can check against these two arrays
IO.Path.GetInvalidFileNameChars
IO.Path.GetInvalidPathChars
-
Jan 5th, 2012, 05:43 PM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|