private void KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
e.Handled = true; 
base.OnKeyPress(e);
}

解决方案 »

  1.   

    以下是只能输入decimal类型的类,如果只要0-9,可以设置小数部分的个数为0.不怕粘贴,汉字using System;
    using System.ComponentModel;
    using System.ComponentModel .Design ;namespace contr
    {
    /// <summary>
    /// 
    /// </summary>
    public class TextBoxNum : System.Windows.Forms.TextBox
    {
    public delegate void ValueChangedHandler(object o,decimal DecValue);
    public  event ValueChangedHandler ValueChanged;
    public int MaxInt=8;
    public int MaxLittle=2;
    public decimal DecimalValue=0;
    public TextBoxNum()
    {

    // 
    // TODO: 在此处添加构造函数逻辑
    //
    }
    private void InitializeComponent()
    {
    }
    protected override void OnTextChanged(EventArgs e)
    {
    if(this.Text .Length !=0&&(this.Text[0])=='.')
    {
    this.Text ="0.";
    this.SelectionStart=(this.Text .Length );
    return;
    }
    string s=this.Text.Trim ();
    if(this.MaxLittle ==0)
    {
    if(s.IndexOf ('.')>0||this.Text .Length >this.MaxInt )
    {
    this.Text =this.ValueDecimal .ToString ();
    this.SelectionStart=(this.Text .Length );
    return; }
    }

    if(s ==""){return;}
    int j=s .IndexOf ('.');
    if((j<0&&s.Length >this.MaxInt )) 
    {

    this.Text =this.ValueDecimal .ToString ();
    this.SelectionStart=(this.Text .Length );
    return;
    }
    if(j>-1&&(s.Length -j-1)>this.MaxLittle)
    {
    this.Text =this.ValueDecimal .ToString ();
    this.SelectionStart=(this.Text .Length );
    return;
    }
    int a=0;

    char[] temp=s.ToCharArray () ;
    for(int i=0;i<s.Length ;i++)
    {
    if(Char.IsDigit(temp[i]))
    {
    continue;
    }
    else if(temp[i]=='.')
    {
    a++;
    if(a>1) 
    {
    this.Text =this.ValueDecimal .ToString ();
    this.SelectionStart=(this.Text .Length );
    return;
    }

    continue;
    }
    this.Text =this.ValueDecimal .ToString ();
    this.SelectionStart=(this.Text .Length );
    return;

    }
    this.ValueDecimal =decimal.Parse (this.Text);
    //TextChanged (e);
    } public event ValueChangedHandler DecmalValueChanged 
    {
    add 
    {
    this.ValueChanged  += value;
    }
    remove 
    {
    this.ValueChanged   -= value;
    }
    }
    public decimal ValueDecimal
    {
    get
    {
    return this.DecimalValue ;
    }
    set
    {
    this.DecimalValue =value;
    if(ValueChanged!=null)
    {
    ValueChanged(this,this.DecimalValue );
    }

    }
    }
    [Browsable(true),DefaultValue(8)]
    public int MaxOfInt
    {
    get
    {
    return this.MaxInt ;
    }
    set
    {
    if(value<=29)
    {
    this.MaxInt =value;
    this.MaxLength =this.MaxLittle +value +1;
    }
    else
    {
    this.MaxInt =29;
    this.MaxLength =this.MaxLittle +29 +1;
    }
    }
    }
    [Browsable(true),DefaultValue(2)]
    public int  MaxOfLittle
    {
    get
    {
    return this.MaxLittle  ;
    }
    set
    {
    if(value<=29)
    {
    this.MaxLittle  =value;
    this.MaxLength =value +this.MaxInt +1;
    }
    else
    {
    this.MaxLittle  =29;
    this.MaxLength =29 +this.MaxInt +1;

    }
    }

    }
    }
    }

      

  2.   

    正则表达式就可以(vb.net改的,不知语法对不)
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            {If Regex.IsMatch(TextBox3.Text, "^\d*$") Then
                MsgBox("OK!!");
            Else
                TextBox3.Text = "";
           }
      

  3.   

    用正则表达 式   查查msdn {d}
      

  4.   

    You can see below:textBox3_keyPress()
    if("0123456789".IndexOf(e.keyChar) == 1 && e.keyChar != (char)8)
    {
      MessageBox.show("no number !");
    e.Handel = true ;
    }its can do it .But i think  正则表达式  best !
      

  5.   

    Sorry : if("0123456789".IndexOf(e.keyChar) == -1  (no 1)
      

  6.   

    用正则表达式,^0-9 匹配的replace就OK,一句代码搞定
      

  7.   

    citylamp(路灯) 的表达式不对,“Regex.IsMatch(TextBox3.Text, "^\d*$")”
    c#不认
     michaelowenii(少年狂) 的方法不能防止非数字的粘贴
      

  8.   

    谁讲讲正则表达式?怎么用?“用正则表达式,^0-9 匹配的replace就OK”何意?
      

  9.   

    知道了,就像玩mud时,用zmud编写机器人一样。呵呵
    using System.Text.RegularExpressions;
    private void button1_Click(object sender, System.EventArgs e)
    {
    Regex digitregex = new Regex("(^[0-9]$)");
    if (digitregex.IsMatch(textBox1.Text))
    {
    MessageBox.Show("OK");
    }
    else
    {
    MessageBox.Show("PLZ Input number[0-9]");
    }
    }
      

  10.   

    最简单的方法:
     Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If Not (IsNumeric(e.KeyChar) Or e.KeyChar = Microsoft.VisualBasic.ChrW(8) Or e.KeyChar = Microsoft.VisualBasic.ChrW(46)) Then
                e.Handled = True
            End If
        End Sub
      

  11.   

    使用RequiredFieldValidator控件
    结合正则表达式!!!-------努力学习 不断实践 虚心讨教--------
      

  12.   

    string str="1233sdfsdf";char myChars[str.length] = str.toCharArray();foreach(char ch in myChars)
    {
       if(ch.IsDigit())
         .....
       else
         .....
    }
      

  13.   

    方法上面也说了很多了
    1可以用ascii判断,
    2也可以用char.IsDigit(),IsNum()等等来判断,
    3也可以通过try{}catch{}把字符转换为数字来判断,如果产生异常那么说明你输入的不是数字了
    方法多的是,还有其他的方法,至于其他的,你可以自己探究
      

  14.   

    你要是在winform程序中,在keydown事件中判断刚输入的字符是不是数字就行,在webform中有个验证控件,能验证输入的是否为数字