I have the below code.
There is a small issue when I typed more than 2 numbers after the “.”, it will keep on prompting me (“No more than two decimal places.”)…
It will also prompt me the message when I have .XX and clicked on backspace.
How can I auto erase the 3rd number after the “.”?
private void textbox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
MessageBox.Show("No space allowed.");
e.Handled = true;
}
string[] array = textbox1.Text.Split(new char[] { '.' });
if (array.Length == 2)
{
if (array[1].Length == 2)
{
MessageBox.Show("No more than two decimal places.");
e.Handled = true;
}
}
}
I used
to stop the Backspace issue.
Now it’s okay.