Click here to Skip to main content
12,698,531 members (23,651 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked

Custom Control: Numeric TextBox: TextBox that alow you to enter only numbers

, 29 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Sometimes we need to control the user input to some specific values. The following article explain how to do this with a TextBox
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


Sometimes we need to control the user input. This control is a normal TextBox, with the property that the control accept only numeric values!

because this control is a TextBox, we inherit from the base class like this NumericTextBox : TextBox

What we have to do is to override the events OnKeyPress and OnKeyDown for the control. The full code is this:

using System.Windows.Forms;
using System.ComponentModel;

namespace MyCustomControls
    [Description("Numeric TextBox")]
    public class NumericTextBox : TextBox
        private bool nonNumberEntered = false;

        public NumericTextBox()
            this.Width = 150;

        protected override void OnKeyPress(KeyPressEventArgs e)
            if (nonNumberEntered == true)
                e.Handled = true;
        protected override void OnKeyDown(KeyEventArgs e)
            nonNumberEntered = false;
            if (e.Shift == true || e.Alt == true)
                nonNumberEntered = true;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                    if (e.KeyCode != Keys.Back)
                        nonNumberEntered = true;

Now, all we have to do is to use it in our application.
This code can be improved. Now it's possible to Paste from clipboard non numeric values. Next version (I'll write this in few days) will contain this improvements!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Ahmed_Said11-Jan-12 11:45
memberAhmed_Said11-Jan-12 11:45 
QuestionPerfect solution Numeric TextBox Pin
Asif Rehman12-Sep-11 0:08
memberAsif Rehman12-Sep-11 0:08 
GeneralThis is better than your Article Pin
Ehsan Golkar28-Sep-08 3:26
memberEhsan Golkar28-Sep-08 3:26 
GeneralNumeric Up/Down Pin
sabrown10018-Oct-07 8:24
membersabrown10018-Oct-07 8:24 
QuestionMSDN?? Pin
jubjubjub18-Aug-07 7:13
memberjubjubjub18-Aug-07 7:13 
GeneralMasked Edit Pin
Mark Nischalke29-Jan-07 8:06
mvpMark Nischalke29-Jan-07 8:06 
GeneralKeyDown not needed [modified] Pin
Luc Pattyn29-Jan-07 7:54
memberLuc Pattyn29-Jan-07 7:54 
GeneralRe: KeyDown not needed Pin
LimitedAtonement2-Nov-09 8:44
memberLimitedAtonement2-Nov-09 8:44 
GeneralUseful Pin
Ilíon29-Jan-07 5:25
memberIlíon29-Jan-07 5:25 
GeneralRe: Useful Pin
zeltera29-Jan-07 5:32
memberzeltera29-Jan-07 5:32 
GeneralRe: Useful Pin
Ilíon29-Jan-07 6:33
memberIlíon29-Jan-07 6:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 29 Jan 2007
Article Copyright 2007 by zeltera
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid