private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsNumber(e.KeyChar) || e.KeyChar == '.')
{
int len = textbox.Text.Length;
bool hasDot = textbox.Text.Contains(".");
if (hasDot && e.KeyChar == '.')
{
e.Handled = true;
}
if (Regex.IsMatch(textbox.Text, "^\\d*\\.\\d{2}$")) e.Handled = true;
}
else e.Handled = e.KeyChar != (char)Keys.Back;
}In this code snippet we give restriction to textbox so it only accepts decimal number up to 2 decimal places.
