Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Text box to accept only number

, 24 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
The following Class is a TextBox for WinForms that only accepts 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (numerics) or backspace as input. Not using regular expressions.using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;namespace...
The following Class is a TextBox for WinForms that only accepts 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (numerics) or backspace as input. Not using regular expressions.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace MyControls
{
    class NumericTextBox : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            // Check if the pressed character was a backspace or numeric.
            if (e.KeyChar != (char)8  && !char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}

License

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

Share

About the Author

Sander Rossel
Software Developer
Netherlands Netherlands
Started programming in 2010 after completing a Bachelor Common Art and Cultural Sciences and a Master Media and Journalism. A decision I haven't come to regret!
A company hired me and they taught me the basics of VB.NET, WinForms and SQL Server.
 
At the end of that year I signed up for CodeProject and that was when my programming knowledge increased rapidly. Being around some of the best and most enthausiastic coders in the world certainly helps you develop your own skills.
 
After a while I enrolled in some IT courses at the Open University and I also got Microsoft Certified.
I am currently working to get a Bachelor degree in IT.
At my job I am programming in VB.NET, C# and SQL Server using WinForms and DevExpress controls.
 
Meanwhile I am trying to educate others through my (prize-winning) articles[^] here on CodeProject and my blog on WordPress[^].

Comments and Discussions

 
GeneralRe: Only valid for certain values of 'everyone'. PinmemberJörgen Andersson16-Jan-12 22:04 
GeneralReason for my vote of 3 Partially works but not complete - s... PinmentorDaveyM694-May-11 10:29 
GeneralYour solution doesn't handle pasting using the mouse or allo... PinmentorDaveyM694-May-11 10:28 
GeneralRe: Obviously I could not make an article for a simple tip/trick... PinmemberNaerling4-May-11 11:10 
GeneralReason for my vote of 1 Out of context. OP is discussing abo... PinmemberVenkatesh Mookkan25-Apr-11 2:10 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... PinmemberNaerling25-Apr-11 2:51 
GeneralReason for my vote of 1 Its not for web. Its for win. PinmemberToniyo Jackson25-Apr-11 1:53 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... PinmemberNaerling25-Apr-11 2:50 
GeneralOP's code is for ASP.Net. But this is for Win forms. PinmemberToniyo Jackson25-Apr-11 1:52 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... PinmemberNaerling25-Apr-11 2:50 
GeneralReason for my vote of 5 Very elegant and practical solution! PinmemberDrABELL24-Apr-11 12:53 
GeneralRe: Thank you! :) PinmemberNaerling24-Apr-11 22:39 

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.141223.1 | Last Updated 24 Apr 2011
Article Copyright 2011 by Sander Rossel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid