1.11M Members

Text box negative numbers

 
0
 

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.

 
0
 

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

 
0
 

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.

 
0
 

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.

 
0
 

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.

 
0
 

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:)

Question Answered as of 2 Years Ago by ddanbe
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article