Hello
Try This
private void TextBox1_GotFocus(object sender, RoutedEventArgs e)
{
try
{
TextBox1.Select(0, TextBox1.Text.Length);
}
catch (System.Exception ex)
{
}
}
private void TextBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
try
{
ValidateTextBox(TextBox1, e);
}
catch (System.Exception ex)
{
}
}
private void ValidateTextBox(System.Windows.Controls.TextBox oTextBox, System.Windows.Input.TextCompositionEventArgs e)
{
try
{
string sAfterDot = string.Empty;
string sInputText = oTextBox.Text;
char c = Convert.ToChar(e.Text);
if (c == '.')
{
if (oTextBox.Text.Contains("."))
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
else
{
if (c == '-')
{
if (oTextBox.Text.Equals(oTextBox.SelectedText))
{
e.Handled = false;
}
else
{
if (oTextBox.Text.Contains("-"))
{
e.Handled = true;
}
else
{
if (!string.IsNullOrEmpty(oTextBox.Text) && oTextBox.Text.LastIndexOf("-") != 0)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
}
}
else
{
if (Char.IsNumber(c))
{
e.Handled = false;
}
else
{
if (c == '-')
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
}
}
if (sInputText.Contains("."))
{
sAfterDot = sInputText.Substring(sInputText.IndexOf("."), (sInputText.Length - sInputText.IndexOf(".")));
if (sAfterDot.Length > 2)
{
if (oTextBox.SelectionStart > sInputText.IndexOf("."))
{
e.Handled = true;
}
}
}
base.OnPreviewTextInput(e);
}
catch (System.Exception ex)
{
}
}