记录一下如何在输入的时候只能输入数字和相关字符,而对别的字符不响应,即对其它字符的e.Handled属性设为True。
private void txtMile_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar == 8) //响应Backspace键 e.Handled = false; else if (e.KeyChar == '.') e.Handled = false; else if (e.KeyChar < '0' || e.KeyChar > '9') { e.Handled = true; } }