You can handle the KeyPress event of the form. The mentioned event take a KeyPressEventArgs as its arguments parameter.
You can handle the KeyPress event of the form. The mentioned event take a KeyPressEventArgs as its arguments parameter. Use the Char.
IsLetterOrDigit function to check the value of the KeyPressEventArgs. KeyChar property. Private void form_KeyPress(object sender, System.Windows.Forms.
KeyPressEventArgs e) { if (char. IsLetterOrDigit(e. KeyChar)) {} else { e.
Handled = false; } } EDIT: You can also try to make a list of your accepted Char values, then check if the preseed character is included in it: private void form_KeyPress(object sender, System.Windows.Forms. KeyPressEventArgs e) { List charList = new List; charList. AddRange(new Char { 'a', 'b' ... }); if (charList.
Contains(e. KeyChar)) {} else { e. Handled = false; } } You may need to consider combining both ways or even more to fulfill your requirements.
Yes, I did go there, but their example isn't very helpful -- with that example they're selecting for just the numbers along the top and in the numberpad of the keyboard, so they can do a range. But, I not only need the alphanumeric characters, but all of the symbols and everything. So, I was racking my brain to try to think up a better solution.
– Isaac May 12 at 20:20 @Isaac: I have edited my answer. You may find something usefull in the appended section. – Akram Shahda May 12 at 20:29 Pretty cool, I hadn't considered that, thank you!
– Isaac May 17 at 19:48.
You could throw all values into a HashSet and check if the KeyCode is in the set. Var invalidKeys = new HashSet { Keys. Shift, Keys.
CapsLock, Keys. Tab, ... Keys. L }; if (!invalidKeys.
Contains(e. KeyCode)) { // Do some work... } Or alternatively, since you're checking for equality, you could just throw all that into a switch statement. Switch (e.
KeyCode) { case Keys. Shift: case Keys. CapsLock: case Keys.
Tab: // ... case Keys. L: break; default: // Do some work... break; }.
Hmm... The Switch case isn't as readable to others because there are a lot of invalid keys to enter... (I'm still trying to figure out which Keycode is the one for the left and right "Alt" keys on the keyboard). But the hashset seems interesting, though Let me give that a try and get back to you. – Isaac May 12 at 20:13 Your hashkey solution ended up working out the best.
:-) I screened for valid and invalid keys, and it works well. Thanks for your help, Jeff Mercado! – Isaac May 17 at 19:44.
You can for example (there are many good attempts) check this page for help on the Char class where you can use methods like IsLetterOrDigit or other functions. Now I could not recognise if you are using Windows Forms? If so, use a simple cast like (char)e.
KeyCode to get the char. Example: private void formControl_KeyModified(object sender, KeyEventArgs e) { char c = (char)e. KeyCode; if (Char.
IsLetterOrDigit(c)) { // useful } // might add more checks // else if (Char. IsPunctuation(c)) ... }.
Yes, it is WinForms development. This is interesting -- so you're saying that I should check for the char of every keycode for each keyboard character that's unnecessary? – Isaac May 12 at 20:15 You should not throw away what you don't need, more just take what you need.
You can try to set e. Handled = true; when you want to not handle the input. – URL1 May 12 at 20:21 I ended up using e.
SuppressKeyPress on those I didn't want also. I went with Jeff's suggestion, but yours had the same logic, and thank you for suggesting it! – Isaac May 17 at 19:46.
If you are concerned with the execution time of the if statement, create a SortedList of the Key values and check if the SortedList contains your key. A possibly better solution is to use the Forms TextBox "TextChanged" event rather than using the KeyDown event.
For my purposes, the TextChanged method isn't going to work -- I'm trying to detect when the form controls have been modified by the user entering data or choosing a different item in a drop-down on a calendar or combobox. However, I have Binding. Parse and Binding.
Format on each control that changes the data into something else and a tab control that causes these to validate very time the tab is changed. So, if I used TextChanged then as soon as the form load it's going to fire this event since B. Parse and B.
Format are validated OnLoad and on SelectIndexChanged of the TabControl, unfort. :-/ – Isaac May 12 at 20:18 Is the SortedList solution sort of like Jeff Mercado's Hash solution? – Isaac May 12 at 20:19 Yes, SortedList and Hash are almost the same, he posted while I was editing my answer.
:-) – AresAvatar May 12 at 20:29 Well, thanks a lot for posting yours. :-) I'm excited to learn anything new, and thanks for your time! – Isaac May 120 at 19:47.
Like @Daniel states in his comment, perhaps white-listing the valid keys is preferable than black-listing all those that are of no interest to you. So if, let's say, you are interested only in letter keys and numbers, you could do it just like it is described in the msdn Keys example if(e. KeyCode > Keys.
NumPad0 && e. KeyCode Keys. D0 && e.
KeyCode Keys. A && e. KeyCode Z) { //do useful stuff here }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.