这个:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
...表明我应该在KeyPress事件中访问e.KeyCode,但似乎我没有。我正在尝试仅允许1、2、3和退格键:
private void textBoxQH1_KeyPress(object sender, KeyPressEventArgs e) {
if ((e.KeyChar != '1') &&
(e.KeyChar != '2') &&
(e.KeyChar != '3') &&
(e.KeyChar != (Keys.Back))) {
e.Handled = true;
}
}
但是“e.”并没有像示例中显示的那样显示“KeyCode”值,并且尝试使用Keys.Back的KeyChar会导致我收到“运算符'!='不能应用于类型为'char'和'System.Windows.Forms.Keys'的操作数”的指责。
那么我该如何实现呢?
(e.KeyChar != (char)8)
? - Gustavo Vargas