您的位置:首页 > 编程语言 > C#

textbox只能输入数字,且长度小于指定值(c#)

2009-06-01 14:13 405 查看
private void textBox1_TextChanged(object sender, EventArgs e) //文本框判断
{
if (textBox1.TextLength > 8)
{
textBox1.Select(0, textBox1.Text.Length - 1);
textBox1.Text = textBox1.SelectedText;
textBox1.SelectAll();
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.Focus();
}

string txt = this.textBox1.Text;
int i = txt.Length;
if (i < 1) return;
for (int m = 0; m < i; m++)
{
string str = txt.Substring(m, 1);
if (!char.IsNumber(Convert.ToChar(str)))
{
this.textBox1.Text = this.textBox1.Text.Replace(str, ""); //将非数字文本过滤掉
this.textBox1.SelectionStart = this.textBox1.Text.Length;//将光标定位到最后一位
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: