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