我想在textBox1.Text里只输入数字或字符怎么写?马上给分 数字if(!(char.IsNumber(e.KeyChar) ||e.KeyChar == '\b'||e.KeyChar == '.')){ e.Handled = true;} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考http://blog.csdn.net/FlashElf/archive/2004/10/31/161024.aspx?Pending=true 以下是自己做的控件的一段代码 希望对你有帮助 //控件中按下键盘时发生 private void txtBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { switch(this.elasticType) { case ElasticType.Number: //****数字模式****// //判断是否为数字 if((Convert.ToInt32(e.KeyChar)>47&&Convert.ToInt32(e.KeyChar)<58)||Convert.ToInt32(e.KeyChar)==8) { return; } if(e.KeyChar=='.') //判断小数点 { if(this.txtBox.Text.Length==0) //判断是否是第一个字符 { this.txtBox.Text="0."; } else if(this.txtBox.Text.IndexOf('.')==-1) //判断是否有小数点 { return; } } e.Handled=true; this.txtBox.SelectionStart=this.txtBox.Text.Length; break; case ElasticType.Word: //****文本模式****// //判断是否为英文字符 if(Convert.ToInt32(e.KeyChar)<65||(Convert.ToInt32(e.KeyChar)>90&&Convert.ToInt32(e.KeyChar)<97)||Convert.ToInt32(e.KeyChar)>122) { //判断空格和退格键 if((Convert.ToInt32(e.KeyChar)==32&&blank)||Convert.ToInt32(e.KeyChar)==8) { return; } e.Handled=true; this.txtBox.SelectionStart=this.txtBox.Text.Length; return; } break; case ElasticType.NumberAndWord: //****混合模式****// //判断空格 if(Convert.ToInt32(e.KeyChar)==32&&!blank) { e.Handled=true; this.txtBox.SelectionStart=this.txtBox.Text.Length; return; } break; } } WPF和webBrower做无边框浏览器? SOAP协议的调用问题(Soap客户端) C# 修改消息框按扭的文本 COM 组建问题。 dataGridView序号问题。 Excel!!!!Excel!!!!Excel中数据转化!!!!COM C#里面的dataGridView1 点击上笔和下笔问题 c#检索Word中的特殊字符 怎样把一个长度为1的string类型转成char? 请问用c#开发网页脚本,如果不懂asp是不是很麻烦 一个超难的问题 原来MAPI。DLL中在。Net下有没有对应的类?(内详)
//控件中按下键盘时发生
private void txtBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
switch(this.elasticType)
{
case ElasticType.Number: //****数字模式****//
//判断是否为数字
if((Convert.ToInt32(e.KeyChar)>47&&Convert.ToInt32(e.KeyChar)<58)||Convert.ToInt32(e.KeyChar)==8)
{
return;
}
if(e.KeyChar=='.') //判断小数点
{
if(this.txtBox.Text.Length==0) //判断是否是第一个字符
{
this.txtBox.Text="0.";
}
else if(this.txtBox.Text.IndexOf('.')==-1) //判断是否有小数点
{
return;
}
}
e.Handled=true;
this.txtBox.SelectionStart=this.txtBox.Text.Length;
break;
case ElasticType.Word: //****文本模式****//
//判断是否为英文字符
if(Convert.ToInt32(e.KeyChar)<65||(Convert.ToInt32(e.KeyChar)>90&&Convert.ToInt32(e.KeyChar)<97)||Convert.ToInt32(e.KeyChar)>122)
{
//判断空格和退格键
if((Convert.ToInt32(e.KeyChar)==32&&blank)||Convert.ToInt32(e.KeyChar)==8)
{
return;
}
e.Handled=true;
this.txtBox.SelectionStart=this.txtBox.Text.Length;
return;
}
break;
case ElasticType.NumberAndWord: //****混合模式****//
//判断空格
if(Convert.ToInt32(e.KeyChar)==32&&!blank)
{
e.Handled=true;
this.txtBox.SelectionStart=this.txtBox.Text.Length;
return;
}
break;
}
}