private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{ if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == 13)) { e.Handled = false; } else { e.Handled = true; return;//將 Handled 設(shè)置為 true,,以取消 KeyPress 事件。添加Return直接退出到方法外部,,不使用的話,,繼續(xù)運(yùn)行下面的程序 } MessageBox.Show(e.KeyChar.ToString());
}
|
|