views:

1315

answers:

5

Hello, 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;
        }
    }
+1  A: 

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
Could you not just use !Char.IsControl(e.KeyChar) and !Char.IsNumber(e.KeyyChar) instead of != true?
Lucas Jones
@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
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
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
Agreed about tabs, but I prefer be more strict, and allow only backspace and nothing more.
arbiter
JimDel
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
Thanks for the follow up answer Fredrik. Good to know.
JimDel
A: 
private void amount_PaidTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
     if (!Char.IsNumber(e.KeyChar) && e.KeyCode != Keys.Back)
          e.Handled = True;
}
colithium
KeyPressEventArgs does not contains KeyCode property.
arbiter
arbiter is correct; this code will not compile.
Fredrik Mörk
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
+1  A: 

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
This does not compile (KeyPressEventArgs does not have a KeyCode property).
Fredrik Mörk
Fixed... I had propagated an error from another answer.
richardtallent
+1  A: 

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.

+3  A: 

Correct answer is:

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