Posts Tagged ‘文本框’

WPF里如何屏蔽数字以外的字符,只允许输入数字(或者小数点)

这段代码是为了解决WPF里如何屏蔽数字以外的字符,只允许输入数字(或者小数点)的问题。 在。cs源文件添加如下事件之后,再把事件附加到要应用的Textbox里上。具体就是点击Textbox,在右下角切换到Events这个tab页面里,选择相应的KeyDown事件和TextChanged事件。 #region Added by Will for TextBox: Numbers input only 屏蔽数字以外的字符 private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { TextBox txt = sender as TextBox; //屏蔽非法按键 if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal) { //if (txt.Text.Contains(“.”) && e.Key == Key.Decimal) if (e.Key == Key.Decimal)

Read the rest of this entry »