数字
if(!(char.IsNumber(e.KeyChar) ||e.KeyChar == '\b'||e.KeyChar == '.'))
{
   e.Handled = true;
}

解决方案 »

  1.   

    参考http://blog.csdn.net/FlashElf/archive/2004/10/31/161024.aspx?Pending=true
      

  2.   

    以下是自己做的控件的一段代码 希望对你有帮助
            //控件中按下键盘时发生
    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;
    }
    }