Click here to Skip to main content
11,705,762 members (53,470 online)
Click here to Skip to main content

Tagged as

WinForms TextBox to accept numbers in a range

, 7 Jan 2012 CPOL 40.3K 8
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.

You may also be interested in...

Comments and Discussions

 
AnswerThe solution [accept only numbers is here] Pin
musadiqqureshi2-May-13 18:42
membermusadiqqureshi2-May-13 18:42 
Generalnice post Pin
Prathap Reddy K2-Jan-12 23:58
memberPrathap Reddy K2-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
muhammadhussain2-Jan-12 19:42
membermuhammadhussain2-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
mentorTom 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
memberPIEBALDconsult30-Dec-11 3:35 
GeneralRe: I agree with the NumericUpDown if I have control over the UI... Pin
Marcus Kramer2-Jan-12 3:23
memberMarcus Kramer2-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
mentorDaveyM6928-Sep-11 6: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
Web01 | 2.8.150819.1 | Last Updated 7 Jan 2012
Article Copyright 2011 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid