日期:2014-05-17 浏览次数:20811 次
用keyDown事件 private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key != Key.Back && e.Key < Key.D0 || e.Key > Key.D9 && e.Key < Key.NumPad0 || e.Key > Key.NumPad9) { e.Handled = true; } }
------解决方案--------------------
/// <summary> /// 限制输入数字以外的字符 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textbox_KeyPress(object sender, KeyPressEventArgs e) { if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && e.KeyChar != 8) { e.Handled = true; } }