你应该用CvtInt32(textBox.text.trim());
判断成功否,解决问题了。
记着给我分。

解决方案 »

  1.   

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
    if(e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
    textBox1.ReadOnly = true;
    textBox1.BackColor = SystemColors.Window;
    }
    }private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
    textBox1.ReadOnly = false;
    }
      

  2.   

    强烈同意 cherno(且歌且狂) :         种不好地不是因为锄头锈,拉不出屎你也别怨茅坑臭
        
      

  3.   

    这里介绍一下我的方法。先写一如下函数ValiText。
    再在文本框内添加如下一句,你喜欢别人只能输入什么字符都行!private void TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    e.Handled= adoconn.ValiText (e.KeyChar.ToString()  ,"0123456789.",true ,this.TextBox1.Text.ToString().Trim() ,true);
    }public static bool ValiText(string KeyIn,string ValidateString,bool Editable,string Txt,bool IsNum)
    {
    //通过 keypress 事件的 e.Handled= ValiText (e.KeyChar.ToString()  ,"0123456789.",true ,txtedit0.Text ,true);

    string Validatelist;   
    if ((Txt.IndexOf(".")>=0)&&(KeyIn==".")&&IsNum )
    return true;
    if (KeyIn=="-"&&Txt.Length >0  )
    return true;
    if (Editable)
    Validatelist=ValidateString.ToUpper()+Convert.ToChar(8);
    else
     Validatelist=ValidateString.ToUpper()  ; //转成大写
    if (Validatelist.IndexOf(Convert.ToChar(KeyIn),0)<0 )
    return true;
    else
    return false;
    }
      

  4.   


     你可以 把form的属性KeyPreview设为true,这样在任何一个 form中的控件发生的键盘事件
     都首先被 form 捕获,在 form 的 KeyDown 里面判定 按键 通常可以用 e.KeyCode 来判定按下的字符 ,如果非法字符可以通过使  e.Handle = false 来使控件忽略 键盘输入