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

Sander Rossel is a professional developer with a working experience in .NET (VB and C#, WinForms, MVC, Entity Framework), JavaScript and SQL Server.

He has an interest in various technologies inlcuding, but not limited to, Functional Programming, NoSQL and Software Design.

He seeks to educate others on his blog, Sander's bits - Writing the code you need, and on his CodeProject profile.

Follow on   Twitter

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