Click here to Skip to main content
15,867,141 members
Articles / Web Development / HTML
Alternative
Tip/Trick

Text box to accept only number

Rate me:
Please Sign up or sign in to vote.
4.33/5 (12 votes)
24 Apr 2011CPOL 62.7K   7   12
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.

C#
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)


Written By
CEO JUUN Software
Netherlands Netherlands
Sander Rossel is a Microsoft certified professional developer with experience and expertise in .NET and .NET Core (C#, ASP.NET, and Entity Framework), SQL Server, Azure, Azure DevOps, JavaScript, MongoDB, and other technologies.

He is the owner of JUUN Software, a company specializing in custom software. JUUN Software uses modern, but proven technologies, such as .NET Core, Azure and Azure DevOps.

You can't miss his books on Amazon and his free e-books on Syncfusion!

He wrote a JavaScript LINQ library, arrgh.js (works in IE8+, Edge, Firefox, Chrome, and probably everything else).

Check out his prize-winning articles on CodeProject as well!

Comments and Discussions

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