天天看点

C#WinForms中限制文本框只能输入数字

给指定TextBox添加KeyPress事件:

代码如下:

private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar ==(char)8)

            {

                return;

            }

            if (e.KeyChar > (char)47 && e.KeyChar < (char)58)

            {

                e.Handled = false;

            }

            else

            {

                e.Handled = true;

            }

        }

继续阅读