TextBox只能输入数字(0..9)

 1.利用控件的PreviewTextInput事件。如下图

TextBox只能输入数字(0..9)_第1张图片

 

2. C# 文件

        //颜色输入控制
        private void txbColors_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //限制只能输入允许的字符
            if ("0123456789".IndexOf(e.Text) == -1)
                e.Handled = true;
        }

你可能感兴趣的:(TextBox只能输入数字(0..9))