Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
Hi..
I want user to enter only numeric values in textbox.
I got this code:
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
     int isNumber = 0;
     e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
}
but i am not getting textbox_KeyPress event.
 
Whats the solution?
Posted 17-May-10 21:16pm
Edited 17-May-10 21:39pm
Abhinav S379.3K
v2
Comments
GeekBond at 2-Nov-12 12:56pm
   
You should use the KeyDown or PreviewKeyDown event. Plus, the code you got is not for WPF, it's for WinForm.
Nelek at 2-Nov-12 13:04pm
   
Have you noticed that the question is from 2010?
GeekBond at 3-Nov-12 8:56am
   
Oops. Hadn't noticed. :O
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try this.
 
private void NumericTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (!Char.IsDigit((char)KeyInterop.VirtualKeyFromKey(e.Key)) & e.Key != Key.Back | e.Key == Key.Space)
    {
        e.Handled = true;
        MessageBox.Show("I only accept numbers, sorry. :(", "This textbox says...");
    }
}
 

You may need to import System.Windows.Input to get KeyInterop
 
The code segment above goes into the PreviewKeyDown event of the TextBox.
  Permalink  
Comments
Parteek1991 at 17-Nov-12 5:53am
   
Thankyou Sir...
GeekBond at 17-Nov-12 7:43am
   
Happy to help. :)
CFQüeb at 27-Jun-13 18:52pm
   
This approach seems good, but allows capture the shift key, causing that characters !"#$%&/()=?¡ becomes to be captured.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

KeyPress in winforms was just perfect for input parsing,
unfortunately WPF don't have such event ;(
here my metod for parsing textbox input, allowing to input signed decimal number
(code is sucks but i cant find simpler way)
private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!char.IsNumber(e.Text, e.Text.Length-1))
            {
                e.Handled = true;
            }
 
            if (( (e.Text).ToCharArray()[e.Text.Length - 1] == '.') || ((e.Text).ToCharArray()[e.Text.Length - 1] == ',')) 
            {
                e.Handled = true;
                if (!(((TextBox)sender).Text.Contains('.')))
                {
                    if (((TextBox)sender).Text.Length == 0) { ((TextBox)sender).Text = "0."; ((TextBox)sender).CaretIndex = ((TextBox)sender).Text.Length; }
                    else { ((TextBox)sender).Text += "."; ((TextBox)sender).CaretIndex = ((TextBox)sender).Text.Length; }
                }
            }
            if ((e.Text).ToCharArray()[e.Text.Length - 1] == '-' & !((TextBox)sender).Text.Contains('-')) { e.Handled = true; ((TextBox)sender).Text = "-" + ((TextBox)sender).Text; ((TextBox)sender).CaretIndex = ((TextBox)sender).Text.Length; }
            if ((e.Text).ToCharArray()[e.Text.Length - 1] == '+' & ((TextBox)sender).Text.Contains('-')) { e.Handled = true; ((TextBox)sender).Text=((TextBox)sender).Text.Substring(1); ((TextBox)sender).CaretIndex = ((TextBox)sender).Text.Length; }
 }
  Permalink  
v2
Comments
GeekBond at 2-Nov-12 12:51pm
   
There is a simpler way, look at my answer.
Bijay Kant Salotry at 13-Nov-13 8:14am
   
Thank you...your solution works for me.
Clifford Nelson at 2-Nov-12 18:56pm
   
Using the PreviewTextInput is not a good idea. KeyDown event works just fine. Also, much too complex for what was asked for. Don't have to worry about signs, or decimal points.
CFQüeb at 27-Jun-13 18:57pm
   
Thank you for this awesome code.. really useful. If someone don't need manage the signs or decimal points.. that adapt the code for his requirements.. we are developers... :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Validations in WPF should be handled using validation rules.
Have a look at this article.
  Permalink  
Comments
Clifford Nelson at 2-Nov-12 18:48pm
   
That is not necessarily true since cannot deal with context issues with this technology. It works for simple validation, but not validation against other values. Not my vote of 1.
louie_nz at 7-Nov-14 18:47pm
   
Maybe it's out of date, but responsibly speaking, so far WPF vaidation rules should be the better solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello,
Try my code its simple
 
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
//allows only number
            if (char.IsNumber(e.KeyChar) == false)
            {
                e.Handled = true;
            }
//backspace working code
            if (e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
            }
        }
 
Do rate my answer once you find it useful
Thanks & Regards
Radix Rose | [Rose]
  Permalink  
Comments
Souvik Banerjee at 21-Jun-10 1:10am
   
Reason for my vote of 1
@Radix: Your code looks like a code for .NET 2.0. In KeyPressEventArgs in WPF, there is no e.KeyChar.
GeekBond at 2-Nov-12 11:50am
   
Your code would only work in WinForms, sorry.

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 2 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100