处理textBox1_KeyPress 事件
private void textBox1_KeyPress(ref object sender, ref system.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar >'9' || e.KeyChar < '0' && e.KeyChar != (char)15 && e.KeyChar != (char)8 ) e.Handled = true;
}
private void textBox1_KeyPress(ref object sender, ref system.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar >'9' || e.KeyChar < '0' && e.KeyChar != (char)15 && e.KeyChar != (char)8 ) e.Handled = true;
}
解决方案 »
- 大侠请进,在理解下,截图打印类
- 输入的字符串格式不正确
- 不规则分页是问题
- 在局域网中,如果一个客户端更新了服务器上数据库表m中的记录后,其它客户端如何及时知道表m被更改了?服务器能自动及时发出通知信息让客户端知道吗?
- 关于如何获取数据的问题
- 送分问题,如何在控件触发事件时,获得该控件名称
- 用枚举怎么判断用户性别?
- datagridview RowPostPaint 事件
- RowFilter 后 子集与原始数据怎样同步?高分奉上
- 有关搜索的问题?
- 如何将邮件头中的各种日期格式都转换成我们常用的日期格式?在线等待中......
- 请问Formula one在form窗体上怎样和SQLServer数据库连接??
{
return;
}
if(e.KeyChar>57 || e.KeyChar<48)
{
this.Focus();
e.Handled=true;
}
{
if(!( (e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)13 || e.KeyChar == (char)8) )
{
e.Handled = true;
}
//
}
//输入不是0~9之间的数字或者不是回退健就不显示
if ((e.KeyChar < (char)48 || e.KeyChar > (char)57) && e.KeyChar != (char)8)
{
e.Handled = true;
}
如果还要控制操作者通过“Ctrl + V”或者“复制菜单”向TextBox框拷贝非数字的字符串可以在TextBox的TextChanged事件中追加如下代码,另外需要追加一个变量来保存拷贝前的字符串,而且需要在Form_Load事件中将TextBox的值赋给该变量。
string sOldValue = "";
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
//取得当前TextBox的值
string sCurVvalue = this.textBox1.Text.Trim().ToString(); try
{
//如果当前的值不是null或者空串时,转换成int型数据
if (sCurVvalue != null && !sCurVvalue.Equals(""))
{
int.Parse(sCurVvalue);
} //没有出现异常,说明是数字,重新给TextBox赋值(不带有空格)
this.textBox1.Text = sCurVvalue;
//更新原来的值
sOldValue = sCurVvalue;
}
catch
{
//出现异常,将TextBox还原成原来的值
this.textBox1.Text = sOldValue;
}
}
private void textBox1_KeyPress(ref object sender, ref system.Windows.Forms.KeyPressEventArgs e)
{
string str="0123456789";
if(str.Index(e.KeyChar)==-1&& e.KeyChar != (char)15 && e.KeyChar != (char)8 )
e.Handled = true;
}