Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,

I have been trying to find out which number I need to use in order for my text box to allow a negative value input. I've tried all the suggested ones I can find; 189, 109 and Key.Subtract, but none of these are working for me.

Below is the code in which I am trying to put it.

Can anyone please help me?

Thank you and many kind regards.

What I have tried:

char ch = e.KeyChar;

            if (!Char.IsDigit(ch) && ch != 8 && ch != 46 && ch != 110 && ch != 189 && ch != 13)
            {
                e.Handled = true;
                MessageBox.Show("Please Enter Valid Input");
            }
Posted
Updated 26-Mar-17 10:45am
Comments
[no name] 26-Mar-17 14:23pm    
96
Member 13068046 26-Mar-17 14:26pm    
Sorry, just tried it and that's not it either. Thank you though.
[no name] 26-Mar-17 14:52pm    
What? You mean the ASCII charts are all wrong? You should tell them so they correct the charts!

Put a breakpoint on the "if" line.
Run your app in the debugger.
Click on the TextBox.
Press '-'.
Look at the content of ch
 
Share this answer
 
Creating a numeric only TextBox isn't that simple.

Do you have code in place to handle copying and pasting from the textbox? Probably not.

Sure, you're limiting key presses, but how about pasting in a non-numeric value?

How about the arrow keys?

Delete and Backspace?

By the way, you should be making a new TextBox control that only accepts values. I sure hope you're not putting this code in your Form.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900