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.

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.