c# 控制textbox 只允许输入 数字 0~9

来源:互联网 发布:cf辅助瞄准软件 编辑:程序博客网 时间:2024/06/04 19:05

在textbox 添加 keypress 属性
如果想显示数字位数,在textbox 属性中 maxlen 选择

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)        {            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') e.Handled = true;            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) e.Handled = true;        }