try
{
int.parse(youtextBox.Text);
}
catch
{
//不是数字
}
{
int.parse(youtextBox.Text);
}
catch
{
//不是数字
}
解决方案 »
- 请问Top、Right、Left、Bottom哪些是只读的
- c#winform textbox输入字符后光标如何自动跳到下一个textbox
- DropDownList,急用~!
- 如何在点击gridview中的按钮控件时,触发onrowdeleting的事件
- WinFrom GridView控制第1列不可编辑?
- 请问,处理WM_NCPAINT消息以重画标题栏后,在Vista系统中,标题栏是否仍然透明?谢谢!
- 控件位置不能设置
- 如何定位鼠标位置.....
- 关于Replace替换为什么没有用
- 自定义控件问题
- 在C#.NET中如何计算指定字体的字符串的长度(以像素为单位)?
- 如何向DB2中插入和读取BLOB字段?
e.Handled = CheckInteger(e);private bool CheckInteger(System.Windows.Forms.KeyPressEventArgs e)
{
int i = (int)e.KeyChar;
if ((( i < 48) || (i >57))
&& (i != 8))
return true;
else
return false;
}
{
if ( e.KeyChar > (char)47 && e.KeyChar < (char)58 || e.KeyChar ==(char)8 ||e.KeyChar==(char)46)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar < (char)46 || e.KeyChar > (char)57) && (e.KeyChar != (char)8 || e.KeyChar == (char)47))
{
e.Handled = true;
}
else
e.Handled = false; }在文本框再加入TextChang事件加入(只能输入"."号一次,文本框有中文时取消中文内容):
private void textBox2_TextChanged(object sender, System.EventArgs e)
{
int i;
char One = '0';
int newone = 0;
string Oone = textBox2.Text.Trim();
string Two = "";
for (i=0;i<textBox2.TextLength;i++)
{
One = char.Parse(Oone.Trim().Substring(i,1).ToString());
if((newone <= 0) && (One =='.'))
{
Two += One;
newone=1;
}
else
{
if(char.IsDigit(One))
{
Two += One;
}
} }
textBox2.Text = Two;
}
{
MessageBox.Show ("无效的输入,请核对!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
jhComboBox.Focus ();
jhComboBox.SelectAll ();
return;
}
{
if ( e.KeyChar > (char)47 && e.KeyChar < (char)58 || e.KeyChar ==(char)8 ||e.KeyChar==(char)46)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
方法2:using System.Text.RegularExpressions;Regex digitregex = new Regex(@"^[0-9]\d*[.]?\d*$");
if (!digitregex.IsMatch(TextBox1.Text))
{
TextBox1.Text="";
MessageBox.Show("只能输入数字!","提示");
}
[email protected]
> {
> if(MessageBox.Show("你输入的不是数字,要进行下一步操作吗!","系统提示",MessageBoxButtons.YesNo)==DialogResult.No)
> {
> MyText.Focus();
> e.Cancel=true;
> }
> else
> {
> e.Cancel=false;
> }
> }
[email protected]
&& (i != 8))
return true;
else
return false;
在TextBox的TextChanged事件里:
string curStr = txt.Text.Trim();
try{
Convert.ToDouble(curStr);
preStr = curStr ;
}catch{
txt.Text = preStr ;
}
{ //Your Code}
else
{MessageBox("Illegal Input");}