如何让TextBox只能输入数字、汉字、字母?
2009-01-10 21:34
579 查看
在编程的过程中我们可能经常会用到TextBox只接受数字输入(或者其他,比如:汉字,字母,等等),这个时候我们可能需要重新封装一个
TextBox(其他方法当然也可以),经常看到有人问这个问题,今天抽了一点时间将此封装做了一下,现在共享大家,希望能给大家带来一定的帮助,如有不
妥敬请斧正,另外:如需转载请注明出处:
alimama_pid="mm_10249644_1605763_5018464";
alimama_type="f";
alimama_sizecode ="tl_1x1_8";
alimama_fontsize=12;
alimama_bordercolor="FFFFFF";
alimama_bgcolor="FFFFFF";
alimama_titlecolor="0000FF";
alimama_underline=0;
alimama_height=22;
alimama_width=0;
![](http://z.alimama.com/alimamal.php?i=mm_10249644_1605763_5018464&u=http%3A%2F%2Fblog.csdn.net%2Fgisfarmer%2Farchive%2F2009%2F01%2F07%2F3728214.aspx&w=&h=22&re=1280x800&sz=tl_1x1_8&r=http%3A%2F%2Fblog.csdn.net%2Fgisfarmer%2Farchive%2F2009%2F01%2F08%2F3733153.aspx&cg=eb27b9a62cb510ea106e6c7aebbacf86&pro=91121573&cas=pro&cah=770&caw=1280&ccd=32&ctz=8&chl=2&cja=1&cpl=18&cmm=55&cf=10.0&sx=297&sy=366&cbw=1390&cbh=0)
只能输入数字(int型)的代码(直接复制就OK了)
public class IntTextBox : System.Windows.Forms.TextBox
{
private int selectPos = 0;
public IntTextBox()
: base()
{
this.BackColor = Color.Beige;
this.TextChanged += new EventHandler(this.TextChage);
this.Leave += new EventHandler(this.FocusLeave);
}
//焦点发生改变时,此处你可以根据自己需要自己选择
public void FocusLeave(object sender, System.EventArgs e)
{
if (this.Text != "")
{
this.Text = ToDBC(this.Text);
if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
MessageBox.Show("输入内容不合法!", "输入提示");
this.Focus();
}
else
{
this.BackColor = Color.Beige;
}
}
}
//内容发生改变时
public void TextChage(object sender, System.EventArgs e)
{
selectPos = this.SelectionStart;
if (this.Text != "")
{
//此处采用郑州表达式
alimama_pid="mm_10249644_1605763_4930558";
alimama_type="f";
alimama_sizecode ="tl_1x5_8";
alimama_fontsize=12;
alimama_bordercolor="FFFFFF";
alimama_bgcolor="FFFFFF";
alimama_titlecolor="0000FF";
alimama_underline=0;
alimama_height=22;
alimama_width=512;
![](http://z.alimama.com/alimamal.php?i=mm_10249644_1605763_4930558&u=http%3A%2F%2Fblog.csdn.net%2Fgisfarmer%2Farchive%2F2009%2F01%2F07%2F3728214.aspx&w=512&h=22&re=1280x800&sz=tl_1x5_8&r=http%3A%2F%2Fblog.csdn.net%2Fgisfarmer%2Farchive%2F2009%2F01%2F08%2F3733153.aspx&cg=70468ddafbf023f71358745c6ff01e88&prq=91122852&cas=prq&cah=770&caw=1280&ccd=32&ctz=8&chl=2&cja=1&cpl=18&cmm=55&cf=10.0&sx=549&sy=980&cbw=1390&cbh=0)
if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
this.SelectionStart = selectPos;
}
else
{
this.BackColor = Color.Beige;
this.SelectionStart = selectPos;
}
}
else
{
this.BackColor = Color.Beige;
}
}
//全角转换成半角,此功能也可以选择
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
}
下面代码是只能输入浮点数(至于其他的比如汉字、字母等等原理一样)
alimama_pid="mm_10249644_1605763_4930558";
alimama_type="f";
alimama_sizecode ="tl_1x5_8";
alimama_fontsize=12;
alimama_bordercolor="FFFFFF";
alimama_bgcolor="FFFFFF";
alimama_titlecolor="0000FF";
alimama_underline=0;
alimama_height=22;
alimama_width=512;
public class FloatTextBox : System.Windows.Forms.TextBox
{
private int selectPos = 0;
public FloatTextBox()
: base()
{
this.BackColor = Color.Beige;
this.TextChanged += new EventHandler(this.TextChage);
this.Leave += new EventHandler(this.FocusLeave);
}
//焦点发生改变时
public void FocusLeave(object sender, System.EventArgs e)
{
if (this.Text != "")
{
this.Text = ToDBC(this.Text);
if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
MessageBox.Show("输入内容不合法!", "输入提示");
this.Focus();
}
else
{
this.BackColor = Color.Beige;
}
}
}
//内容发生改变时
public void TextChage(object sender, System.EventArgs e)
{
selectPos = this.SelectionStart;
if (this.Text != "")
{
if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
this.SelectionStart = selectPos;
}
else
{
this.BackColor = Color.Beige;
this.SelectionStart = selectPos;
}
}
else
{
this.BackColor = Color.Beige;
}
}
//全角转换成半角
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
}
TextBox(其他方法当然也可以),经常看到有人问这个问题,今天抽了一点时间将此封装做了一下,现在共享大家,希望能给大家带来一定的帮助,如有不
妥敬请斧正,另外:如需转载请注明出处:
alimama_pid="mm_10249644_1605763_5018464";
alimama_type="f";
alimama_sizecode ="tl_1x1_8";
alimama_fontsize=12;
alimama_bordercolor="FFFFFF";
alimama_bgcolor="FFFFFF";
alimama_titlecolor="0000FF";
alimama_underline=0;
alimama_height=22;
alimama_width=0;
只能输入数字(int型)的代码(直接复制就OK了)
public class IntTextBox : System.Windows.Forms.TextBox
{
private int selectPos = 0;
public IntTextBox()
: base()
{
this.BackColor = Color.Beige;
this.TextChanged += new EventHandler(this.TextChage);
this.Leave += new EventHandler(this.FocusLeave);
}
//焦点发生改变时,此处你可以根据自己需要自己选择
public void FocusLeave(object sender, System.EventArgs e)
{
if (this.Text != "")
{
this.Text = ToDBC(this.Text);
if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
MessageBox.Show("输入内容不合法!", "输入提示");
this.Focus();
}
else
{
this.BackColor = Color.Beige;
}
}
}
//内容发生改变时
public void TextChage(object sender, System.EventArgs e)
{
selectPos = this.SelectionStart;
if (this.Text != "")
{
//此处采用郑州表达式
alimama_pid="mm_10249644_1605763_4930558";
alimama_type="f";
alimama_sizecode ="tl_1x5_8";
alimama_fontsize=12;
alimama_bordercolor="FFFFFF";
alimama_bgcolor="FFFFFF";
alimama_titlecolor="0000FF";
alimama_underline=0;
alimama_height=22;
alimama_width=512;
if (!(new Regex(@"^-?/d+$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
this.SelectionStart = selectPos;
}
else
{
this.BackColor = Color.Beige;
this.SelectionStart = selectPos;
}
}
else
{
this.BackColor = Color.Beige;
}
}
//全角转换成半角,此功能也可以选择
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
}
下面代码是只能输入浮点数(至于其他的比如汉字、字母等等原理一样)
alimama_pid="mm_10249644_1605763_4930558";
alimama_type="f";
alimama_sizecode ="tl_1x5_8";
alimama_fontsize=12;
alimama_bordercolor="FFFFFF";
alimama_bgcolor="FFFFFF";
alimama_titlecolor="0000FF";
alimama_underline=0;
alimama_height=22;
alimama_width=512;
public class FloatTextBox : System.Windows.Forms.TextBox
{
private int selectPos = 0;
public FloatTextBox()
: base()
{
this.BackColor = Color.Beige;
this.TextChanged += new EventHandler(this.TextChage);
this.Leave += new EventHandler(this.FocusLeave);
}
//焦点发生改变时
public void FocusLeave(object sender, System.EventArgs e)
{
if (this.Text != "")
{
this.Text = ToDBC(this.Text);
if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
MessageBox.Show("输入内容不合法!", "输入提示");
this.Focus();
}
else
{
this.BackColor = Color.Beige;
}
}
}
//内容发生改变时
public void TextChage(object sender, System.EventArgs e)
{
selectPos = this.SelectionStart;
if (this.Text != "")
{
if (!(new Regex(@"^/d+(/./d+)?$")).IsMatch(this.Text))
{
this.BackColor = Color.OrangeRed;
this.SelectionStart = selectPos;
}
else
{
this.BackColor = Color.Beige;
this.SelectionStart = selectPos;
}
}
else
{
this.BackColor = Color.Beige;
}
}
//全角转换成半角
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
}
相关文章推荐
- asp.net 限制textbox只能输入数字,并且防止启用输入法来输入字母或汉字
- 【转】一劳永获的解决textbox 只能输入数字、汉字、字母等问题
- 一劳永获的解决textbox 只能输入数字、汉字、字母等问题
- 验证textbox无法输入全角,汉字,字母,只能输入数字
- 解决textbox 只能输入数字、汉字、字母
- 正则表达式限制文本框只能输入数字,小数点,英文字母,汉字
- 正则表达式限制文本框只能输入数字,小数点,英文字母,汉字
- 如何使TextBox只能输入数字?
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- js正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- 正则表达式限制文本框只能输入数字,小数点,英文字母,汉字
- 只能输入数字,字母,汉字和下划线
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- js 控制只能输入数字、只能输入字母和汉字等
- javascript/JS限制用户名只能输入 汉字字母和数字下划线
- [转]用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- js正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码