Click here to Skip to main content
Licence 
First Posted 29 Jan 2007
Views 35,350
Bookmarked 12 times

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

By | 29 Jan 2007 | Article
Sometimes we need to control the user input to some specific values. The following article explain how to do this with a TextBox

Introduction

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;
                return;
            }
            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!

License

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

zeltera

Engineer

Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberAhmed_Said10:45 11 Jan '12  
QuestionPerfect solution Numeric TextBox PinmemberAsif Rehman23:08 11 Sep '11  
GeneralThis is better than your Article PinmemberEhsan Golkar2:26 28 Sep '08  
GeneralNumeric Up/Down Pinmembersabrown1007:24 18 Oct '07  
QuestionMSDN?? Pinmemberjubjubjub6:13 18 Aug '07  
GeneralMasked Edit PinmvpMark Nischalke7:06 29 Jan '07  
GeneralKeyDown not needed [modified] PinmemberLuc Pattyn6:54 29 Jan '07  
GeneralRe: KeyDown not needed PinmemberLimitedAtonement7:44 2 Nov '09  
GeneralUseful PinmemberIlíon4:25 29 Jan '07  
GeneralRe: Useful Pinmemberzeltera4:32 29 Jan '07  
GeneralRe: Useful PinmemberIlíon5:33 29 Jan '07  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 29 Jan 2007
Article Copyright 2007 by zeltera
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid