Click here to Skip to main content
11,482,402 members (71,547 online)
Click here to Skip to main content

Text box to accept only number

, 24 Apr 2011 CPOL 19K 7
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   LinkedIn

Comments and Discussions

 
GeneralRe: Only valid for certain values of 'everyone'. Pin
Jörgen Andersson16-Jan-12 22:04
memberJörgen Andersson16-Jan-12 22:04 
GeneralReason for my vote of 3 Partially works but not complete - s... Pin
DaveyM694-May-11 10:29
mentorDaveyM694-May-11 10:29 
GeneralYour solution doesn't handle pasting using the mouse or allo... Pin
DaveyM694-May-11 10:28
mentorDaveyM694-May-11 10:28 
GeneralRe: Obviously I could not make an article for a simple tip/trick... Pin
Naerling4-May-11 11:10
memberNaerling4-May-11 11:10 
GeneralReason for my vote of 1 Out of context. OP is discussing abo... Pin
Venkatesh Mookkan25-Apr-11 2:10
memberVenkatesh Mookkan25-Apr-11 2:10 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... Pin
Naerling25-Apr-11 2:51
memberNaerling25-Apr-11 2:51 
GeneralReason for my vote of 1 Its not for web. Its for win. Pin
Toniyo Jackson25-Apr-11 1:53
memberToniyo Jackson25-Apr-11 1:53 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... Pin
Naerling25-Apr-11 2:50
memberNaerling25-Apr-11 2:50 
GeneralOP's code is for ASP.Net. But this is for Win forms. Pin
Toniyo Jackson25-Apr-11 1:52
memberToniyo Jackson25-Apr-11 1:52 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... Pin
Naerling25-Apr-11 2:50
memberNaerling25-Apr-11 2:50 
GeneralReason for my vote of 5 Very elegant and practical solution! Pin
DrABELL24-Apr-11 12:53
memberDrABELL24-Apr-11 12:53 
GeneralRe: Thank you! :) Pin
Naerling24-Apr-11 22:39
memberNaerling24-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.150520.1 | Last Updated 24 Apr 2011
Article Copyright 2011 by Sander Rossel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid