- js中escape()函数和unescape()函数的功能
- 小程序-跳转页面的方法及坑
- 怎么在button事件中找到gridview每行的控件?
- 【C#】IndexOf、LastIndexOf、Substring的用法
- ASP.NET 中的“外部组件发生异常”错误解决过程
- char、varchar、text和nchar、nvarchar、ntext的区别
- c#.net常用函数和方法集
- 先运行接口代码还是先运行实现了接口的代码
- 很不错的一篇有关ASP.NET Session的分析文章
- php中的超全局变量
邮箱:
手机:15383239821
C#textBox文本框限制
1.正整数
//可以输入正整数
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8) { e.Handled = true; }
}
2.整数
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 45) { e.Handled = true; }
if ((int)e.KeyChar == 45)
{
//如果选中文本框中的所有文本,则可以输入负号
if (textBox.SelectionLength == textBox.TextLength) { }
else
{
//如果文本框的第一个字符被选中,则可以输入负号
if (textBox.SelectionStart == 0) { }
else
{
if (textBox.TextLength != 0)
{
e.Handled = true;
}
}
}
}
}
3.超出范围限制自动退格
可以用两种方法触发:
1.textBox_TextChange:每输入一个字符,都会验证
2.textBox_KeyDown:当摁下某个键时,验证(下例是回车键)
//可以输入小于100的整数
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox.Text != "" && textBox.Text != "-")
{
if (Convert.ToInt32(textBox.Text) < 100)
{
MessageBox.Show("请输入小于100的整数", "提示");
string str = textBox_ControlRobot_TravelAngleControl_TravelDistance.Text;
str = str.Substring(0, str.Length - 1);
textBox.Text = str;
textBox.SelectionStart = textBox.TextLength;
}
}
}
}
4.只能输入汉字
输入其它字符没有反应
private void txtRealName_KeyPress(object sender, KeyPressEventArgs e)
{
Regex rg = new Regex("^[\u4e00-\u9fa5]$");
if (!rg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b')
{
e.Handled = true;
}
}
5.只能输入字母和数字
private void txtPass_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
|| (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
6.不能少于8位
private void txtPass_Validating(object sender, CancelEventArgs e)
{
if (txtPass.TextLength < 8)
{
MessageBox.Show("密码不能小于8位", "提示");
return;
}
}
-
2023-09-17php怎么将汉语转为拼音?
-
2020-07-29企业新网站SEO优化工作应该从哪些方面着手
-
2013-04-15怎样添加MX记录解析
-
2010-07-20网站的页面关键字密度优化
-
2013-06-29解决SQL2000 MMC无法创建管理单元
-
2013-06-10ASP.NET对文件的操作,创建文件,判断文件是否存在,判断文件是否存在删除文件夹
