Click here to Skip to main content
13,138,857 members (55,668 online)
Click here to Skip to main content
Add your own
alternative version

Stats

44.7K views
7 bookmarked
Posted 24 Apr 2011

Text box to accept only number

, 24 Apr 2011
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), HTML, CSS, JavaScript, Oracle and SQL Server.

He has an interest in various technologies including, but not limited to, Functional Programming, NoSQL, Software Design, Continuous Integration and, more generally, Software Quality.

He wrote two ebooks which you can download for free: Object-Oriented Programming in C# Succinctly and SQL Server for C# Developers Succinctly

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

You may also be interested in...

Comments and Discussions

 
GeneralRe: Only valid for certain values of 'everyone'. Pin
Jörgen Andersson16-Jan-12 21:04
memberJörgen Andersson16-Jan-12 21:04 
GeneralReason for my vote of 3 Partially works but not complete - s... Pin
DaveyM694-May-11 9:29
mentorDaveyM694-May-11 9:29 
GeneralYour solution doesn't handle pasting using the mouse or allo... Pin
DaveyM694-May-11 9:28
mentorDaveyM694-May-11 9:28 
GeneralRe: Obviously I could not make an article for a simple tip/trick... Pin
Naerling4-May-11 10:10
memberNaerling4-May-11 10:10 
GeneralReason for my vote of 1 Out of context. OP is discussing abo... Pin
Venkatesh Mookkan25-Apr-11 1:10
memberVenkatesh Mookkan25-Apr-11 1:10 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... Pin
Naerling25-Apr-11 1:51
memberNaerling25-Apr-11 1:51 
GeneralReason for my vote of 1 Its not for web. Its for win. Pin
Toniyo Jackson25-Apr-11 0:53
memberToniyo Jackson25-Apr-11 0:53 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... Pin
Naerling25-Apr-11 1:50
memberNaerling25-Apr-11 1:50 
GeneralOP's code is for ASP.Net. But this is for Win forms. Pin
Toniyo Jackson25-Apr-11 0:52
memberToniyo Jackson25-Apr-11 0:52 
GeneralRe: This was actually a reply to the Alternate of Charles Hesing... Pin
Naerling25-Apr-11 1:50
memberNaerling25-Apr-11 1:50 
GeneralReason for my vote of 5 Very elegant and practical solution! Pin
DrABELL24-Apr-11 11:53
memberDrABELL24-Apr-11 11:53 
GeneralRe: Thank you! :) Pin
Naerling24-Apr-11 21:39
memberNaerling24-Apr-11 21:39 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 24 Apr 2011
Article Copyright 2011 by Sander Rossel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid