点击所需设置的文本框,单击事件,双击KeyPress,输入下面代码
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == ' ' || e.KeyChar == ',' || e.KeyCher ==(char)8)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
限制输入数字和小数点
private void txtmoney_KeyPress(object sender, KeyPressEventArgs e)
{
string str = txtmoney.Text;
int a = e.KeyChar;
if (a >= 48 && a <= 57 || a == 46 || a == 13 || a == 8)
{
if (a == 46 && str.Contains("."))
e.Handled = true;
else
{
if (a == 46 && str == "")
{
txtmoney.Text = "0";
txtmoney.SelectionStart = 1;
}
else
e.Handled = false;
}
if (str.IndexOf('.')!=-1 && (str.IndexOf('.') + 3) == str.Length)
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
获取按键ASCII码
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char a = e.KeyCher;
int i = a;
textBox1.Text = i.Tostring();
}
textBox文本框限制只输入数字
- 格式:txt
- 大小:1.51 KB
- 文档页数:1