Click here to Skip to main content
Click here to Skip to main content

Tagged as

WinForms TextBox to accept numbers in a range

, 7 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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)
         {
            return;
         }
 
         int num;
         if (Int32.TryParse(this.Text, out num))
         {
            if (num > mMax)
            {
               this.Text = mPrevious;
               this.Select(this.Text.Length, 0);
            }
         }
         else
         {
            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.

License

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

Share

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.
Follow on   LinkedIn

Comments and Discussions

 
AnswerThe solution [accept only numbers is here] Pinmembermusadiqqureshi2-May-13 19:42 
Generalnice post PinmemberPrathap Reddy K3-Jan-12 0:58 
GeneralReason for my vote of 2 throws error on paste(Ctrl v with so... PinmemberSelvin2-Jan-12 23:44 
GeneralRe: This case is handled now. PinmemberManish K. Agarwal3-Jan-12 0:05 
GeneralReason for my vote of 3 nice work Pinmembermuhammadhussain2-Jan-12 20:42 
GeneralReason for my vote of 3 I don't really think this is the bes... PinmentorTom Clement2-Jan-12 8:31 
GeneralRe: Thanks for your input. 1. Negative numbers are not supported... PinmemberManish K. Agarwal3-Jan-12 0:07 
GeneralI'll stick with a NumericUpDown. PinmemberPIEBALDconsult30-Dec-11 4:35 
GeneralRe: I agree with the NumericUpDown if I have control over the UI... PinmemberMarcus Kramer2-Jan-12 4:23 
GeneralReason for my vote of 5 Good one, sir. Simple and useful Pinmemberdanlobo30-Dec-11 0:22 
GeneralReason for my vote of 5 Good, very short and simple way PinmemberShlabh Sharma27-Sep-11 3:16 
QuestionA few comments... PinmentorDaveyM6928-Sep-11 7:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 7 Jan 2012
Article Copyright 2011 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid