Click here to Skip to main content
14,543,787 members

WinForms TextBox to accept numbers in a range

Rate this:
4.43 (12 votes)
Please Sign up or sign in to vote.
4.43 (12 votes)
7 Jan 2012CPOL
Simple Numeric TextBox to accept digits for a given minimum maximum range
Sometime back, I was looking for a TextBox control which can silently accept numbers (positive int only) for a given range (minimum and maximum). I implemented the following simple class to achieve this:

class NumberTextBox : System.Windows.Forms.TextBox
   private string mPrevious;
   private int mMin;
   private int mMax;

   public NumberTextBox() : this(0, Int32.MaxValue) { }
   public NumberTextBox(int min, int max)
      : base()
      if ((min > max) || min < 0 || max < 0)
         throw new Exception("Minimum and maximum values are not supported");
      mMin = min;
      mMax = max;
      this.Text = min.ToString();

   protected override void OnKeyPress(KeyPressEventArgs e)
      mPrevious = this.Text;
      e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);

   protected override void OnTextChanged(EventArgs e)
      if (this.Text == string.Empty)

      int num;
      if (Int32.TryParse(this.Text, out num))
         if (num > mMax)
            this.Text = mPrevious;
            this.Select(this.Text.Length, 0);
         this.Text = mPrevious;
         this.Select(this.Text.Length, 0);

   protected override void OnLeave(EventArgs e)
      int num;
      if (!Int32.TryParse(this.Text, out num) || num < mMin || num > mMax)
         this.Text = mPrevious;
         // To move the cursor at last
         this.Select(this.Text.Length, 0);

To use this NumberTextBox on your dialog/form, include the above class in the project and replace TextBox with NumberTextBox in your form.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish K. Agarwal
Software Developer (Senior) Oracle
India India
Working with Oracle. Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 13+ years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product and large scale enterprises software development.

Comments and Discussions

AnswerThe solution [accept only numbers is here] Pin
musadiqqureshi2-May-13 18:42
Membermusadiqqureshi2-May-13 18:42 
Generalnice post Pin
k.Prathap2-Jan-12 23:58
professionalk.Prathap2-Jan-12 23:58 
GeneralReason for my vote of 2 throws error on paste(Ctrl v with so... Pin
Selvin2-Jan-12 22:44
MemberSelvin2-Jan-12 22:44 
GeneralRe: This case is handled now. Pin
Manish K. Agarwal2-Jan-12 23:05
MemberManish K. Agarwal2-Jan-12 23:05 
GeneralReason for my vote of 3 nice work Pin
M.Hussain.2-Jan-12 19:42
MemberM.Hussain.2-Jan-12 19:42 
GeneralReason for my vote of 3 I don't really think this is the bes... Pin
Tom Clement2-Jan-12 7:31
professionalTom Clement2-Jan-12 7:31 
GeneralRe: Thanks for your input. 1. Negative numbers are not supported... Pin
Manish K. Agarwal2-Jan-12 23:07
MemberManish K. Agarwal2-Jan-12 23:07 
GeneralI'll stick with a NumericUpDown. Pin
PIEBALDconsult30-Dec-11 3:35
professionalPIEBALDconsult30-Dec-11 3:35 
GeneralRe: I agree with the NumericUpDown if I have control over the UI... Pin
fjdiewornncalwe2-Jan-12 3:23
professionalfjdiewornncalwe2-Jan-12 3:23 
GeneralReason for my vote of 5 Good one, sir. Simple and useful Pin
danlobo29-Dec-11 23:22
Memberdanlobo29-Dec-11 23:22 
GeneralReason for my vote of 5 Good, very short and simple way Pin
Shlabh Sharma27-Sep-11 2:16
MemberShlabh Sharma27-Sep-11 2:16 
QuestionA few comments... Pin
DaveyM6928-Sep-11 6:49
professionalDaveyM6928-Sep-11 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 27 Sep 2011

Tagged as


9 bookmarked