3

I have a textbox that I would like for only numbers. But if I hit the wrong number, I cant backspace to correct it. How can I allow backspaces to work. Thanks

    private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Char.IsNumber(e.KeyChar) != true)
        {
            e.Handled = true;
        }
    }
JimDel
  • 4,309
  • 11
  • 54
  • 99

9 Answers9

3

You could add a check to allow control characters as well:

if (Char.IsControl(e.KeyChar) != true && Char.IsNumber(e.KeyChar) != true)
{
    e.Handled = true;
}

Update: in response to person-b's comment on the code s/he suggests the following style (which is also how I would personally write this):

if (!Char.IsControl(e.KeyChar) && !Char.IsNumber(e.KeyChar))
{
    e.Handled = true;
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 1
    Could you not just use !Char.IsControl(e.KeyChar) and !Char.IsNumber(e.KeyyChar) instead of != true? – Lucas Jones Jun 29 '09 at 22:12
  • @person-b: that is how I would personally write it, but I chose to let my code follow JimDel's convention to focus on functionality rather than coding style. – Fredrik Mörk Jun 29 '09 at 22:15
  • I don't think that control characters is allowed in this case. For example tab is control character, but as I understand author need only numbers. – arbiter Jun 29 '09 at 22:59
  • Tab will not be an issue in a regular TextBox; it doesn't even trigger the KeyPress event unless you set the AcceptsTab property to true (and then you have sort of opted in on that behaviour). Most other control characters that can be produces with the keyboard (such as line feed, carriage return) does not have any effect in a single line text box. I think allowing control characters would work very well. – Fredrik Mörk Jun 29 '09 at 23:10
  • Agreed about tabs, but I prefer be more strict, and allow only backspace and nothing more. – arbiter Jun 29 '09 at 23:16
  • Thanks Fredrik. I could have sworn I tried that but with with no luck. I pasted your code and added "&& Char.IsPunctuation(e.KeyChar) != true" so it would also accept a decimal point. This works great. Thanks again. But what does !Char do? Does it just shorten the statement? – JimDel Jun 30 '09 at 00:16
  • The ! character in !Char.IsNnn is a logical negation operator (essentially meaning "not"); it's shorter to write !expression than expression != true or expression == false. Read more: http://msdn.microsoft.com/en-us/library/f2kd6eb2(VS.80).aspx – Fredrik Mörk Jun 30 '09 at 07:16
  • Thanks for the follow up answer Fredrik. Good to know. – JimDel Jun 30 '09 at 19:17
3

Correct answer is:

private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !сhar.IsNumber(e.KeyChar) && (e.KeyChar != '\b');
}
arbiter
  • 9,447
  • 1
  • 32
  • 43
1

You can also override the TextChanged-event:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string text = (sender as TextBox).Text;

    StringBuilder builder = new StringBuilder(String.Empty);

    foreach (char character in text)
    {
        if (Char.IsDigit(character))
        {
            builder.Append(character);
        }
    }

    (sender as TextBox).Text = builder.ToString();
}

Please note that you would have to add in code to set the caret position.

user112889
  • 805
  • 9
  • 15
0
private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
     if (!Char.IsNumber(e.KeyChar) && e.KeyCode != Keys.Back)
          e.Handled = True;
}
colithium
  • 10,269
  • 5
  • 42
  • 57
  • KeyPressEventArgs does not contains KeyCode property. – arbiter Jun 29 '09 at 22:46
  • Oh my mistake, I did it from memory and put it in your method signature. Use KeyDown or Up, not Press because e.KeyChar doesn't have access to Delete. – colithium Jun 30 '09 at 04:15
0

A slightly cleaner format of the above:

 private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e)
 {
   e.Handled = !(Char.IsNumber(e.KeyChar) || (e.KeyChar==Keys.Back));
 }
richardtallent
  • 34,724
  • 14
  • 83
  • 123
0
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
    {
        // Allow Digits and BackSpace char
    }        
    else
    {
        e.Handled = true;
    }
}

Refer Link also: Masking Textbox to accept only decimals decimals/12209854#12209854

Community
  • 1
  • 1
andy
  • 5,979
  • 2
  • 27
  • 49
0

You can use the following on Key Press event:

      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!Char.IsDigit(e.KeyChar) & (e.KeyChar != 8)) e.Handled = true; 
    }
0

I found the following worked well. It included extra exceptions for allowing backspace, arrow keys delete key etc. How to allow only numeric (0-9) in HTML inputbox using jQuery?

Community
  • 1
  • 1
Valamas
  • 24,169
  • 25
  • 107
  • 177
-1

Press on the your textbox then enter the events From events double click on KeyPress And then write this code

if( e.KeyChar <'0' || e.KeyChar > '9' )
if(e.KeyChar!=08)
e.Handled=true;