I insert this on KeyPress event:
e.Handled = !Char.IsNumber(e.KeyChar);
But I don't have the Backspace key, how to fix it?
How about:
e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
Or equivalently:
e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8;
(As in roman's answer, you can use '\b' instead of 8 in the above code too.)
here's how to check if backspace was pressed:
if(e.KeyChar == '\b'){//backspace was pressed}
backspace key
e.KeyChar == (char) Keys.Back
Related
Until today for validate what data I insert in the text box I used this following code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// allows 0-9, backspace, and decimal
if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46))
{
e.Handled = true;
return;
}
// checks to make sure only 1 decimal is allowed
if (e.KeyChar == 46)
{
if ((sender as TextBox).Text.IndexOf(e.KeyChar) != -1)
e.Handled = true;
}
}
It works perfectly fine, but when I try to select all, or copy or paste and also insert "-" for negative float it's just crash, how can I validate this type of "KeyPress"?
Try this, it should do the trick:
private void txtFloat1_KeyPress(object sender, KeyPressEventArgs e)
{
// allows 0-9, backspace, and decimal
if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46 && e.KeyChar != 45 && e.KeyChar != 3 && e.KeyChar != 22 && e.KeyChar != 1))
{
e.Handled = true;
return;
}
// checks to make sure only 1 decimal is allowed
if (e.KeyChar == 46)
{
if ((sender as TextBox).Text.IndexOf(e.KeyChar) != -1)
e.Handled = true;
}
}
I blocked all key on keyboard excluding 1-9 and I have a problem how to enable comma?
My code:
private void textbox_KeyDown(object sender, KeyRoutedEventArgs e)
if (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9 || e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9 || e.Key == Windows.System.VirtualKey.Decimal)
{
e.handled = false;
}
else
{
e.handled = true;
}
This code will allow only numbers and comma
if (!char.IsDigit(e.KeyChar) && e.KeyChar != ',')
{
e.Handled = true;
}
Alternative
if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != ',')
{
e.Handled = true;
}
try this...
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if((e.KeyChar >= 48 && e.KeyChar <= 57) || (e.KeyChar >= 97 && e.KeyChar <= 105))
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
According to the documentation for Virtual Key Codes you need OemComma which is 0xBC or (VirtualKey)188.
You can try this:
string keyInput = e.KeyChar.ToString();
if (Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if (e.KeyChar == '\b')
{
// Backspace key is OK
}
else if (e.KeyChar == ',')
{
// Comma key is OK
}
else
{
// Swallow this invalid key and beep
e.Handled = true;
// MessageBeep();
}
First of all you have to remember that decimal mark ( either . or , ) has to be set up in the CultureInfo. If you're planning to release your application to the further audience I would recommend keeping this in mind.
Another thing is that your condition makes no sense :
// this has to be always true
e.Key >= Windows.System.VirtualKey.Number0
&&
e.Key <= Windows.System.VirtualKey.Number9
|| // above or below has to be true
e.Key >= Windows.System.VirtualKey.NumberPad0
&& // something from above has to be true and below has to be true
e.Key <= Windows.System.VirtualKey.NumberPad9
|| // or just decimal mark .. ?
e.Key == Windows.System.VirtualKey.Decimal
So proceeding with the code :
// check for the keys
if(
( // if numeric between 0 and 9
e.Key >= Windows.System.VirtualKey.Number0
&&
e.Key <= Windows.System.VirtualKey.Number9
)
|| // or
( // numeric from numpad between 0 and 9
e.Key >= Windows.System.VirtualKey.NumberPad0
&&
e.Key <= Windows.System.VirtualKey.NumberPad9
)
|| // or decimal mark
e.Key == Windows.System.VirtualKey.Decimal
)
{
// your logic
}
Remember that Windows.System.VirtualKey.Decimal will not return the decimal mark ( separator ) based on the CultureInfo but instead the decimal mark from the numpad.
If you want to use culture info ( international application ) you can find decimal mark in CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator and then compare the text input.
This code does not allow me to enter numeric values from a numeric keypad.
private void textBox1_KeyDown( object sender, KeyEventArgs e ) {
e.SuppressKeyPress = !( (e.KeyValue >= 48 && e.KeyValue <= 57) )
}
How can I include numerical values in general (both from regular and number keys)?
Because after all they're different keys, even though the char they represent is the same.
To get a better result you can use the Keys enum and KeyCode property:
e.SuppressKeyPress = !((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))
Or use KeyPress event because there you'll have char codes in the event args.
Inside a KeyPress event handler you can do:
e.Handled = !Char.IsDigit(e.KeyChar)
They are not the same because they are not the same key. Keyvalue is an abstraction of the specific key you have pressed on the keyboard, not of the value that the key represents.
That said, you can simply check if either the numpad key or the other is pressed with a simple OR.
One solution is using the KeyPress event and TryParse method like this:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int n;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out n);
}
Or you can check for NumPads like this:
e.SuppressKeyPress = !((e.KeyValue >= 48 && e.KeyValue <= 57 || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)));
How can I restrict the user from entering anything, in a text box, other than a value between 0 to 99.99
My code seems to be working but, it will allow any number to be entered. It will allow only number and a dot once. But it will still allow a value greater than 99.99
Below my code:
private void InputMargin_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
// allow only number and dot
if ( ! char.IsControl(e.KeyChar)
&& ! char.IsDigit(e.KeyChar)
&& e.KeyChar != '.'
|| (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
)
{
//e.Handled = true;
double margin;
double.TryParse((sender as TextBox).Text, out margin);
if (margin >= 0 && margin <= 99.99)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
}
catch
{
e.Handled = false;
}
}
why not use NumericUpDown control with DecimalPlaces property set to 2.
no need to add any additional coding like on textbox.
why are you using `&& e.KeyChar != '.' because doing so doesnot accepts '.' .just delete this because you have also used (sender as TextBox).Text.IndexOf('.') > -1 this accepts only one '.' in the whole textfield so the Right if statement will be.
if ( ! char.IsControl(e.KeyChar)
&& ! char.IsDigit(e.KeyChar)
|| (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
)
{
//Your Code
}
Why this recipe is wrong
if (e.KeyChar <= (char)Keys.NumPad0 && e.KeyChar >= (char)Keys.NumPad2)
{
if (e.KeyChar <= (char)Keys.O && e.KeyChar >= (char)Keys.Oem2)
{
MessageBox.Show("Yes");
}
}
I want to be numbers between 0 and 2
You are checking that it's equal or less than 0 and at the same time equal or greater than 2. That's not possible. You have to switch your greater than to less than and and vice versa, and to handle both numpad and other numeric keys, change to this:
if (e.KeyChar >= (char)48 && e.KeyChar <= (char)50)
...
Keys.O
since you cannot start enum name with 0 (number zero) this must be O (letter o)
and whole logic seems flawed, it should be something like
if ((e.KeyChar >= (char)Keys.NumPad0 && e.KeyChar <= (char)Keys.NumPad2) || (e.KeyChar => (char)Keys.Oem0 && e.KeyChar <= (char)Keys.Oem2))
(im not sure about >= and <= and Oem0)
I can't tell whether this code is present in a KeyDown or KeyPress event handler. If you want to filter typing keys then you should use KeyPress. And the code is then simply:
if (e.KeyChar >= '0' && e.KeyChar <= '2') {
MessageBox.Show("yes");
}
Between 0 and 2 ...
e.KeyChar >= (char)Keys.NumPad0 && e.KeyChar <= (char)Keys.NumPad2