private void TextBox_Validation(object sender,CancelEventArgs ce)
  {
    try
    {
      int value=Int32.Parse(this.Text);
    }
    catch(Exception)
    {
      ce.Cancel=true;
      MessageBox.Show("Please Enter Numeric Value");
    }
  }

解决方案 »

  1.   

    System.Single.Parse(TextBoxId.Text);
    System.Decimal.Parse(TextBoxId.Text);
      

  2.   

    // Source Code starts using System;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;  class NumberBox:TextBox
    {
      public NumberBox()
      {
        this.KeyPress+=new KeyPressEventHandler(NumberBox_KeyPress);
      }    private void NumberBox_KeyPress(object sender,KeyPressEventArgs kpe)
      {
        int KeyCode=(int)kpe.KeyChar;      if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46)
        {
          kpe.Handled=true;
        }
        else
        {
          if(KeyCode==46)
          {
            kpe.Handled=(this.Text.IndexOf(".")>-1);
          }
        }
      }    private bool IsNumberInRange(int Val,int Min,int Max)
      {
        return (Val>=Min && Val<=Max);
      }
    } class NumberBoxDemo:Form
    {
      Label l1=new Label();
      NumberBox n1=new NumberBox();
      public NumberBoxDemo()
      {
        l1.Text="Number Box:";
        n1.Location=new Point(l1.Left+l1.Width+10,l1.Top);
        this.Controls.Add(l1);
        this.Controls.Add(n1);
      }    public static void Main()
      {
        Application.Run(new NumberBoxDemo());
      }
    }
    // Source Code End
      

  3.   

    使用用类Convert的一个静态方法就可以了,Convert.ToFloat(),或 Convert.ToDouble()