private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string[] txt = null;
if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && !textBox1.Text.Contains(".") || e.KeyChar == '\b')
{
if (textBox1.Text.Contains("."))
{
txt = textBox1.Text.Split('.');
if (txt[1].Length > 1)
{
if (e.KeyChar >= '0' && e.KeyChar <= '9')
{
e.Handled = true;
}
}
if (txt[0].Length != leftlen)
{
if (e.KeyChar >= '0' && e.KeyChar <= '9')
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
}
else
{
e.Handled = false;
}
}
else
{
e.Handled = true;
}
if (txt != null)
{
leftlen = txt[0].Length;
}
}
Most of the things you want is done in this
Just declare leftlen globally and initialize it on form load to -1
Hope it will solve your problem :)