我有一个文本框,我只想要数字.但如果我打错了号码,我就无法退格以纠正它.如何允许退格键工作.谢谢 private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsNumber(e.KeyCha
private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsNumber(e.KeyChar) != true) { e.Handled = true; } }您可以添加一个检查以允许控制字符:
if (Char.IsControl(e.KeyChar) != true && Char.IsNumber(e.KeyChar) != true) { e.Handled = true; }
更新:响应person-b对代码的评论他/他建议以下样式(这也是我个人写的方式):
if (!Char.IsControl(e.KeyChar) && !Char.IsNumber(e.KeyChar)) { e.Handled = true; }