Click here to Skip to main content
15,902,938 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

WinForm Application: Confine the textbox Only to Take Numeric Inputs

Rate me:
Please Sign up or sign in to vote.
2.76/5 (4 votes)
18 Dec 2016CPOL2 min read 7.1K   7   2
This are a practical ways to confine inputs into a specific range. The natural thinking is to track them and change. That derives two ways below.

Introduction

This tip solves one thing: numeric input only in textbox in WinForm Application.

And the way applied here can also be adapted to serve other needs.

Actually, we can also use other Windows form controls like numeric updown or MaskedTextbox, or even make our own textbox control (reference: https://msdn.microsoft.com/en-us/library/ms229644(v=vs.100).aspx) to only accept numeric input. This article will also cover these two solutions.

How to Approach

We need to keep an eye on every user input and if the character input is not numeric, we need to delete it.

Thus using event control is a very natural way. One way is to use TextChanged -- delete the invalid input after it has already been rendered on the textbox; Either is to use KeyPress -- we intercept when the key is down which is before rendering onto textbox.

Writing the Code

Using TextChanged Event

Firstm we add an Event bound to the textbox. Here, the name of mine is "tbNumOnly"

Then, we add this in the event handler here.

(It involves a bit of logic here which is why I do not highly recommend this method.)

C#
private void tbNumOnly_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string str = tbNumOnly.Text;
                int cursorPos = tbNumOnly.SelectionStart;
                if (!char.IsDigit(str[cursorPos - 1]))
                {
                    tbNumOnly.Text = str.Substring(0, cursorPos - 1) + str.Substring(cursorPos);
                    tbNumOnly.SelectionStart = cursorPos - 1;
                    tbNumOnly.SelectionLength = 0;
                } 
            }
            catch (IndexOutOfRangeException)
            {
 
            }
        }

It acquires the cursor position in the textbox first, judges if the character right before the cursor is numeric, discards if not, and finally sets the cursor back to the desired position. If index is out of range, just do nothing.

Using KeyPress Event

C#
private void tbNumOnly_KeyPress(object sender, KeyPressEventArgs e)     
    {           
        char ch = e.KeyChar;    
        if (!char.IsDigit(ch) &&       
             ch != Convert.ToChar(Keys.Back) &&     
             ch != Convert.ToChar(Keys.Delete))                
        e.Handled = true;       
    }

Here, we just monitor the key user pressed in the textbox. If it is is not numeric, we artificially set it Handled hence the key appears as if not yet pressed.

This solution is much easier than the first one.

GOTCHA: Please don't use KeyUp or KeyDown like this: KeyPress includes KeyUp and KeyDown. If KeyDown is Handled, KeyUp is still to be handled, hence the non-numeric character will appear on the textbox. Thus, if you handled KeyPress, you handled both.

Differences

Solution "TextChanged" will delete the invalid input automatically, while solution "KeyPress" will disable any invalid inputs.

 

Drawbacks

Thanks for the reminding of OriginalGriff these two self-implemented ways don't solve the paste problems. You can right-click on it or control + v virtually anything you want. I figured out one way thanks to Damith at his answers at http://stackoverflow.com/questions/16684406/how-to-disable-the-right-click-context-menu-on-textboxes-in-windows-using-c#16684475. This can disable any right-click context menu or shortcuts, which is nevertheless a bit unconvenient if you have other uses of the textbox.

 

tbNumOnly.ShortCutsEnabled = false;

 

Alternatives:

1. Using a numericupdown or mask. They're here:

(also you can find it in All Windows Forms)

Numeric up down is foolproof and For MaskTextbox, we usually set these to easily achieve our goal:

 

2.  "Wrap" a textbox. That is well described as an example in MSDN here: you overriding the basic OnKeyPress() (Keypress event).

You can just paste the two solutions described above into the "OnKeyPress()", after the

base.OnKeyPress(e);

 

To conclude, making a textbox only accept numeric input entails deleting the invalid input after right at then they input.

Hope these help.

License

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


Written By
United States United States
A guy learning .NET C#, ASP.NET, JavaScript, Unity C#, SQL. Glad to be with the community discussing and sharing experience.

Comments and Discussions

 
QuestionUseless... Pin
Maciej Los17-Dec-16 5:11
mveMaciej Los17-Dec-16 5:11 
AnswerRe: Useless... ? NO. Pin
Clivic TR18-Dec-16 0:49
Clivic TR18-Dec-16 0:49 

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.