using System.Text.RegularExpressions;
 
 正则表达式...

解决方案 »

  1.   

    正则表达式是一种,
    也可应用TRY,CATCH来强制转换,如果catch到错误,自然不合法。
      

  2.   


     限制用户输入数字:using System.Text.RegularExpressions; ......string str;......bool yn=Regex.IsMatch(str,@"^[0-9]+$");
    if(yn==true) 
    { MessageBox.Show("y"); //含有数字
    }
    else
    {
    MessageBox.Show("n"); //不含有数字}
      

  3.   

    或者
    try{double.Parse(txtNN.Text)}
      

  4.   

    i am used to use try-catch, thank you all.
    Check out now.
      

  5.   

    public class NumInputBox :System.Windows.Forms.TextBox
    {
    public NumInputBox()
    {
    this.ImeMode = System.Windows.Forms.ImeMode.Disable;
    }
    protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
    {
    if(Char.IsDigit(e.KeyChar)||(e.KeyChar==(char)0x08))
    {
    base.OnKeyPress(e);
    }
    else
    {
    e.Handled = true;
    }
    }
    }