WPF设置文本框只能输入数字


 

 

//前台UI设置
  <TextBox Width="100" Height="30" PreviewTextInput="TextBox_PreviewTextInput"  PreviewKeyDown="TextBox_PreviewKeyDown" InputMethod.IsInputMethodEnabled="False"/>
InputMethod.IsInputMethodEnabled="False"禁用输入法
后台代码限定只能输入数字
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
                e.Handled = true;
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
                e.Handled = true;
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM