textbox输入控制 一个texbox控件,我想控制它的输入,1.只能输6位2.前3个只能为字母,3.后3位自能为数字,怎么做到? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 WebForm里有验证控件,不知道WinForm里有没有提供 用正则来进行校验Regex regex=new Regex(@"^\w{3}\d{3}$"); 在 private void texbox_KeyPress(object sender, KeyPressEventArgs e) { }中通过代码验证限制了 private void Form1_Load(object sender, EventArgs e) { this.textBox1.MaxLength = 6; } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (this.textBox1.Text.Length <3) { if (char.IsLetter(e.KeyChar) == true || char.IsControl(e.KeyChar) == true) { e.Handled = false; } else { e.Handled = true; } } if (this.textBox1.Text.Trim().Length >= 3) { if (char.IsDigit(e.KeyChar) == true || char.IsControl(e.KeyChar) == true) { e.Handled = false; } else { e.Handled = true; } } 先进行输入限制如下:private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (this.textBox1.Text.Trim().Length == 6) { e.Handled = true; } if (this.textBox1.Text.Trim().Length < 3) { if ((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) { e.Handled = true; } } else if (this.textBox1.Text.Trim().Length >=3) { if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) { e.Handled = true; } } }然后在文本框失去焦点时用上面几位的正则来进行判断 这种情况可以考虑用 MaskedTextBox MaskedTextBox1.Mask = "LLL000"; 改为下面的字母支持大小写private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (this.textBox1.Text.Trim().Length == 6) { e.Handled = true; } if (this.textBox1.Text.Trim().Length < 3) { if (((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) && ((e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar != 8) && (e.KeyChar != 46))) { e.Handled = true; } } else if (this.textBox1.Text.Trim().Length >=3) { if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) { e.Handled = true; } } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (this.textBox1.Text.Trim().Length == 6) { e.Handled = true; } if (this.textBox1.Text.Trim().Length < 3) { if (((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) && ((e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar != 8) && (e.KeyChar != 46))) { e.Handled = true; } } else if (this.textBox1.Text.Trim().Length >=3) { if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) { e.Handled = true; } } } 如何编程引用本地rdlc报表中的某列? 请教正则表达式遇到的问题 C#调用dll文件 跪求L 两方通信不管哪方先启动,都能正常连接发送数据 双击 c1TrueDBGrid 的问题 多线程的问题 asp.net保存文件对话框 一道.Netframework的基础理论题目,求详细解答? 打印问题 C#新手提问关于连接数据库的问题?请大家帮帮忙!在线等…… radioButton自动执行Click事件的问题 ◆MVC问题请教◆
Regex regex=new Regex(@"^\w{3}\d{3}$");
private void texbox_KeyPress(object sender, KeyPressEventArgs e)
{
}
中通过代码验证限制了
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.MaxLength = 6;
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.textBox1.Text.Length <3)
{
if (char.IsLetter(e.KeyChar) == true || char.IsControl(e.KeyChar) == true)
{
e.Handled = false;
}
else
{
e.Handled = true;
} }
if (this.textBox1.Text.Trim().Length >= 3)
{
if (char.IsDigit(e.KeyChar) == true || char.IsControl(e.KeyChar) == true)
{
e.Handled = false; }
else
{
e.Handled = true;
}
}
{
if (this.textBox1.Text.Trim().Length == 6)
{
e.Handled = true;
}
if (this.textBox1.Text.Trim().Length < 3)
{
if ((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
}
else if (this.textBox1.Text.Trim().Length >=3)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
}
}然后在文本框失去焦点时用上面几位的正则来进行判断
{
if (this.textBox1.Text.Trim().Length == 6)
{
e.Handled = true;
}
if (this.textBox1.Text.Trim().Length < 3)
{
if (((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) && ((e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar != 8) && (e.KeyChar != 46)))
{
e.Handled = true;
}
}
else if (this.textBox1.Text.Trim().Length >=3)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
}
}
{
if (this.textBox1.Text.Trim().Length == 6)
{
e.Handled = true;
}
if (this.textBox1.Text.Trim().Length < 3)
{
if (((e.KeyChar < 97 || e.KeyChar > 122) && (e.KeyChar != 8) && (e.KeyChar != 46)) && ((e.KeyChar < 65 || e.KeyChar > 90) && (e.KeyChar != 8) && (e.KeyChar != 46)))
{
e.Handled = true;
}
}
else if (this.textBox1.Text.Trim().Length >=3)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
}
}