textbox输入控制

hsie168518 2009-09-02 05:22:32
一个texbox控件,我想控制它的输入,

1.只能输6位
2.前3个只能为字母,
3.后3位自能为数字,

怎么做到?
...全文
85 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
风之影子 2009-09-02
  • 打赏
  • 举报
回复
建议用MaskedTextBox
风之影子 2009-09-02
  • 打赏
  • 举报
回复
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;
}
}
}
风之影子 2009-09-02
  • 打赏
  • 举报
回复
改为下面的字母支持大小写

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;
}
}
}
wuyi8808 2009-09-02
  • 打赏
  • 举报
回复
MaskedTextBox1.Mask = "LLL000";
wuyi8808 2009-09-02
  • 打赏
  • 举报
回复
这种情况可以考虑用 MaskedTextBox
风之影子 2009-09-02
  • 打赏
  • 举报
回复
先进行输入限制如下:
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;
}
}
}

然后在文本框失去焦点时用上面几位的正则来进行判断
Forrest23 2009-09-02
  • 打赏
  • 举报
回复

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;
}

}




lvanjie 2009-09-02
  • 打赏
  • 举报
回复

private void texbox_KeyPress(object sender, KeyPressEventArgs e)
{
}

中通过代码验证限制了
wiki14 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hsie168518 的回复:]
能不能不说废话
[/Quote]

哥,你又没有问我用什么正则。
^\w{3}\d{3}$
= =
cppfaq 2009-09-02
  • 打赏
  • 举报
回复
用正则来进行校验
Regex regex=new Regex(@"^\w{3}\d{3}$");
cheng_feng001 2009-09-02
  • 打赏
  • 举报
回复
WebForm里有验证控件,不知道WinForm里有没有提供
hsie168518 2009-09-02
  • 打赏
  • 举报
回复
能不能不说废话
wiki14 2009-09-02
  • 打赏
  • 举报
回复
正则表达式
hsie168518 2009-09-02
  • 打赏
  • 举报
回复
winform

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧