private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox b = sender as TextBox;
string result;
if (b.IsSelectionActive)
{
result = b.Text.Remove(b.SelectionStart, b.SelectionLength).Insert(b.SelectionStart, e.Text);
}
else
{
result = b.Text.Insert(b.CaretIndex, e.Text);
}
float f = 0;
if (!float.TryParse(result, out f))
{
e.Handled = true;
}
}