1
Answer

Combobox KeyPress Event

Administrator

Administrator

22y
6.4k
1
I have been looking around the Internet in hopes to find a way to restrict certian keys from being accepeted from a combobox control... I found an article that explains using the KeyPress Event and setting the KeyPressEventArg.Handled property to true. I thought is was heaven until I tried it... On a combobox I could not get it to restrict the keystrokes but when I past the exact same code into a textbox's Keypress event it worked like a charm. N E IDEAS. The following will not restrict numeric values from being entered private void CombopBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ( char.IsDigit ( e.KeyChar ) ) e.Handled = true; } the following does restrict numeric values from being entered private void TextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ( char.IsDigit ( e.KeyChar ) ) e.Handled = true; } I don't get it? Jim Di Crescenzo
Answers (1)
0
Administrator
Admin 2.3k 1.3m 22y
Instead of the keyPress event, try KeyDown or KeyUp events. To a user it is sematics really, but with programming I found that the KeyDown event has a lot more options than keypress. KeyDown allows you to distinguish between a distinct key press or if the key being held down for a period of time, etc. In fact, the only way that I know how to handle the function keys (F1 - F12) is with the KeyDown or KeyUp events.