HI:)

i have tried for several hours to make my textbox accept negative numbers but i couldn't solve the problem so i'm hoping u can help me with this.I got here an example of my code:

private void textBox76_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
    textBox76.MaxLength = 1;
}

My text box accepts only numbers from 0 to 9..so i was wondering how i can make it accept negative numbers to.Thanks for your advices.

Dani AI

Generated

your handler is a WinForms KeyPress handler. Note that e.KeyCode is for KeyDown/KeyUp, not KeyPress, so checking Keys.OemMinus will not fire in the code you showed ( is right if you move the logic to KeyDown). Also, if your valid range is -9..9, do not cap MaxLength at 1; you need room for the minus sign.

A simple, robust pattern is to allow only the final string you want: either one digit, or a leading '-' followed by one digit. Build the text as it would look after the key press (respecting selection), then decide to accept or reject it.

private void textBox76_KeyPress(object sender, KeyPressEventArgs e)
{
    var tb = (TextBox)sender;

    // always allow control keys (Backspace, Delete, etc.)
    if (char.IsControl(e.KeyChar)) return;

    // what will the text look like after this key?
    string proposed = tb.Text.Remove(tb.SelectionStart, tb.SelectionLength)
                             .Insert(tb.SelectionStart, e.KeyChar.ToString());

    bool ok = (proposed.Length == 1 && char.IsDigit(proposed[0])) ||
              (proposed.Length == 2 && proposed[0] == '-' && char.IsDigit(proposed[1]));

    e.Handled = !ok;   // block anything else
}

This keeps '+' and other punctuation out, allows a single leading '-', and handles replace/selection cases correctly. You can drop MaxLength entirely, or set it to 2 as an extra guard.

Side note: the later snippet you tried with char.IsLower() ends up letting symbols like '+' through because it only blocks lowercase letters. Validating the whole proposed string avoids that trap.

If you want to skip key filtering altogether, the built-in NumericUpDown control is even easier: set Minimum = -9 and Maximum = 9. Or validate on the Validating event with int.TryParse and a range check.

Recommended Answers

All 5 Replies

To test for a minus you could use e.KeyCode == Keys.OemMinus
Why are you setting MaxLength to 1 ?

To test for a minus you could use e.KeyCode == Keys.OemMinus
Why are you setting MaxLength to 1 ?

because i'm creating some kind of game and the numbers must be between -9;9...and unfortunatly it's still not working..can't introduce negative numbers in the textbox.

The property MaxLength has to do with the Lenght of the text you can put into a textbox, not with the value.
So if you make it 1 you can just type 0ne character and no more.

The property MaxLength has to do with the Lenght of the text you can put into a textbox, not with the value.
So if you make it 1 you can just type 0ne character and no more.

yep, got it now "-" is a character....but even like that i can't introduce the "-" sign.I guess i need some kind of validation or something like that.

i have tried for several hours to make my textbox accept negative numbers but i couldn't solve the problem so i'm hoping u can help me with this.I got here an example of my code:

private void textBox76_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
            textBox76.MaxLength = 1;
        }

My text box accepts only numbers from 0 to 9..so i was wondering how i can make it accept negative numbers to.Thanks for your advices.[/QUOTE]

Thanks a lot for the replies. The final code is:

e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && char.IsLower(e.KeyChar) 

now i can introduce + and - thanks a lot:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.