title: Using regex on a on-screen keyboard
body: <p>Ok i'm really getting stuck here guys :/. I've got a (custom!) onscreen keyboard for which I wanna use regular expressions.</p>
  
  <p>I Successfully created a regex to only allow <code>0-9</code> and a <code>comma</code> for a regular keyboard.</p>
  
  <p>Of course this isn't working for my on-screen keyboard.</p>
  
  <p>I have a code snippet that works...</p>
  
  <p>BUT I also have a space button, a backspace button, and 2 buttons to move the caret around.</p>
  
  <p>When I use the below regex code and click the space, backspace or the caret buttons I get this error:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>    // the regex code   
      private Control keyb = null;
      private void EditProdPriceEx_GotFocus(object sender, RoutedEventArgs e)
      {
          string AllowedChars = "[^0-9,]";
          if (Regex.IsMatch(EditProdPriceEx.Text, AllowedChars))
          {
              keyb = (Control)sender;
          }
      }
  </code></pre>
  
  <p>Space button:</p>
  
  <pre><code>    private void KnopSpatie_Click(object sender, RoutedEventArgs e)
      {
          TextBox tb = keyb as TextBox;
          int selStart = tb.SelectionStart; // here is where i get the error
          string before = tb.Text.Substring(0, selStart);
          string after = tb.Text.Substring(before.Length);
          tb.Text = string.Concat(before, " ", after);
          tb.SelectionStart = before.Length + 1;
      }
  </code></pre>
  
  <p>Backspace Button:</p>
  
  <pre><code>    private void KnopWissen_Click(object sender, RoutedEventArgs e)
      {
          TextBox tb = keyb as TextBox;
          int cursorPosition = tb.SelectionStart; // here is where i get the error
          if (cursorPosition &gt; 0)
          {
              tb.Text = tb.Text.Substring(0, cursorPosition -1) + tb.Text.Substring(cursorPosition);
              tb.SelectionStart = cursorPosition - 1;
          }
      }
  </code></pre>
  
  <p>Move caret left button:</p>
  
  <pre><code>    private void KnopLinks_Click(object sender, RoutedEventArgs e)
      {
          TextBox tb = keyb as TextBox;
          if (tb.SelectionStart &gt; 0) // here is where i get the error
          {
              int selStart = tb.SelectionStart;
              string before = tb.Text.Substring(0, selStart);
              string after = tb.Text.Substring(before.Length);
              tb.SelectionStart = before.Length - 1;
          }
      }
  </code></pre>
  
  <p>Move caret right button:</p>
  
  <pre><code>    private void KnopRechts_Click(object sender, RoutedEventArgs e)
      {
          TextBox tb = keyb as TextBox;
  
          if (tb.SelectionStart &gt;= 0) // here is where i get the error
          {
              int selStart = tb.SelectionStart;
              string before = tb.Text.Substring(0, selStart);
              string after = tb.Text.Substring(before.Length);
              tb.SelectionStart = before.Length + 1;
          }
      }
  </code></pre>