You can do this on textbox KeyPress Event Handler:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '.' && textBox1.Text.Contains('.'))
e.Handled = true;
if (char.IsNumber(e.KeyChar) || e.KeyChar == '.')
{
if (Regex.IsMatch(
textBox1.Text,
"^\d*\.\d{2}$")) e.Handled = true;
}
else e.Handled = e.KeyChar != (char)Keys.Back;
}
Basically what it does is it limits the decimal places to two, it also limits decimal point into one. But the backspace should be working fine still.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…