Question: I have a Silverlight app with a textbox whose input I want to limit to decimal numbers only. Searching the web I came across the following possible solution (curiously in different places with different people claiming authorship of the same lines of code) 
It appears to work well except that after at least 1 numeral has been entered it will then allow the letter 'd' in either upper or lower case to be entered, I can't figure out why that is and thus can't figure out how to prevent that from happening. Could anyone please provide a solution. Many thanks.

```
    private void Unit_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.Key == Key.Tab)
        {

        }
        var thisKeyStr = "";
        if (e.PlatformKeyCode == 190 || e.PlatformKeyCode == 110)
        {
            thisKeyStr = ".";
        }
        else
        {
            thisKeyStr = e.Key.ToString().Replace("D", "").Replace("NumPad", "");
        }
        var s = (sender as TextBox).Text + thisKeyStr;
        var rStr = "^[0-9]+[.]?[0-9]*$";
        var r = new Regex(rStr, RegexOptions.IgnoreCase);
        e.Handled = !r.IsMatch(s);

    }

```

Answer: 
You could try the following:

1. Replace the `else` with `else if (e.Key != Key.D)` or
2. set the `Handled` property like this:

```
e.Handled = !r.IsMatch(s) || string.IsNullOrEmpty(thisKeyStr);

// also possible:
e.Handled = !r.IsMatch(s) || e.Key == Key.D;

```
```
      private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        bool isDigit = e.Key >= Key.D0 && e.Key < Key.D9 || e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
        e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 ||e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right;

        if (isDigit) { }
        else
            e.Handled = true; 
    }

```