C# TextBox只允许输入数字

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //开头不允许出现0
            if (textBox1.SelectionStart == 0)
            {
                if (e.KeyChar == '0')
                {
                    e.Handled true;
                    return;
                }
            }


            //只能
            if (e.KeyChar == '0'
                || e.KeyChar == '1'
                || e.KeyChar == '2'
                || e.KeyChar == '3'
                || e.KeyChar == '4'
                || e.KeyChar == '5'
                || e.KeyChar == '6'
                || e.KeyChar == '7'
                || e.KeyChar == '8'
                || e.KeyChar == '9'
                || e.KeyChar == '\b')
            {
                e.Handled false;
            }
            else
            {
                e.Handled true;
            }
        }

 

posted @ 2022-07-26 10:11  devgis  阅读(1100)  评论(0编辑  收藏  举报