WPF TextBox no Space allowed

Viewed 7396

I want a user to only write numbers (0-9) in a TextBox. I'm using following code to prevent the user of writing letters and other characters except of numbers, but I can't avoid the user to use the space in the TextBox.

private void CheckIsNumeric(TextCompositionEventArgs e)
{
    int result;

    if (!(int.TryParse(e.Text, out result)))
    {
       e.Handled = true;
       MessageBox.Show("!!!no content!!!", "Error", 
                       MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }
}

I allready tried using something like

if (Keyboard.IsKeyDown(Key.Space))
{ //...}

but did not succeed.

Thanks for help.

2 Answers
Related