private void txtCost_KeyDown(object sender, KeyEventArgs e)
{
if (!string.IsNullOrWhiteSpace(this.txtCost.SelectedText) && this.IsNumber(e.Key))
{
return;
}
string cost = this.txtCost.Text;
if (!this.IsNumber(e.Key))
{
e.Handled = true;
return;
}
if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
{
e.Handled = cost.IndexOf(".") != -1;
return;
}
if (e.Key == Key.Subtract || e.Key == Key.OemMinus)
{
e.Handled = cost.IndexOf("-") != -1;
return;
}
if (e.Key != Key.Tab && cost.IndexOf(".") != -1)
{
int littleNumberCount = cost.Split('.')[1].Length;
e.Handled = littleNumberCount >= 2;
return;
}
}