1

i want to make a TextBox numeric. The Idea is to check the pressed Key before the Text is shown in the TextBox. I implement the Method KeyDown() of the TextBox. Here is my Code:

private void txtX_KeyDown(object sender, KeyEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
    {
        e.Handled = true;
    }
}

KeyEventArgs has no KeyChar (I got this from a tutorial)

I use WPF

Rene Winter
  • 23
  • 1
  • 1
  • 4
  • I think you're going about it the wrong way, have a look at this question: http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf – Ray Dec 30 '11 at 10:12
  • Anyway, what's your actual question? KeyEventArgs has a Key property to see what is pressed. – Ray Dec 30 '11 at 10:14
  • 1
    Don't reinvent the wheel. There are plenty of examples of numeric textboxes on the Internet. – ken2k Dec 30 '11 at 10:14
  • 1
    [KeyPress](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx) event has [KeyPressEventArgs](http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx) parameter which contains KeyChar property. Resume - use KeyPress event instead KeyDown – Renatas M. Dec 30 '11 at 10:19
  • @Reniuz He's using [WPF TextBox](http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox_events.aspx). There is no KeyPress event. – Ray Dec 30 '11 at 10:27
  • @Ray so where did SO get idea about KeyChar property? – Renatas M. Dec 30 '11 at 10:47
  • @Reniuz Who's SO? He read a tutorial for WinForms when he's using WPF (as tagged by the question) which is the whole problem. – Ray Dec 30 '11 at 10:55

5 Answers5

2

For windows form

e.SuppressKeyPress = !(e.KeyValue >= 48 && e.KeyValue <= 57); For wpf e.Handled = !("D1D2D3D4D5D6D7D8D9D0".Contains(e.Key.ToString())); Im using VS2010 Using the same logic U can do a better solution, more elegant :)
0

Use this code.

private void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !("D1D2D3D4D5D6D7D8D9D0".Contains(e.Key.ToString()));  
}
Kalu Singh Rao
  • 1,671
  • 1
  • 16
  • 21
0

Then you do this:

        if (e.Key == Key.D)
            e.Handled = true;
        else
            e.Handled = !("D1D2D3D4D5D6D7D8D9D0".Contains(e.Key.ToString()));

:D

Kicasd
  • 91
  • 1
  • 4
0

I consider that KeyEventArgs don't have KeyChar property. You can check the

KeyEventArgs defintion as below.

public class KeyEventArgs : EventArgs
{
    // Fields
    private bool handled;
    private readonly Keys keyData;
    private bool suppressKeyPress;

    // Methods
    public KeyEventArgs(Keys keyData);

    // Properties
    public virtual bool Alt { get; }
    public bool Control { get; }
    public bool Handled { get; set; }
    public Keys KeyCode { get; }
    public Keys KeyData { get; }
    public int KeyValue { get; }
    public Keys Modifiers { get; }
    public virtual bool Shift { get; }
    public bool SuppressKeyPress { get; set; }
}

And you can try to covert key value to char, please reference the sample as below.

private string keyChar = string.Empty;

// Convert KeyValue to char
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
    keyChar = (char)e.KeyValue;
}
JKhuang
  • 1,523
  • 2
  • 12
  • 14
0

If you are using WinForms, the easiest way to filter characters are in the KeyPress event.

private void txtX_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
Espen Burud
  • 1,871
  • 10
  • 9