Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will start with the code

C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && !textBox2.Text.Contains(".") || e.KeyChar == '\b')
    {
        if (textBox2.Text.Contains("."))
        {
            string[] txt = textBox2.Text.Split('.');

            if (txt[1].Length > 1)
            {
                if (e.KeyChar >= '0' && e.KeyChar <= '9')
                {
                    e.Handled = true;
                }
            }
        }
        else
        {
            e.Handled = false;
        }
    }
    else
    {
        e.Handled = true;
    }
}


Explain: Basicaly what it does: when someone types to textBox2 it lets insert only numeric symbols, backspace and one dot (.), it also defines that if dot is inserted there can only be inserted two numbers after it.

The problem: When I enter digit example. 5.22 ir aby other with dot and two number after it, then i selec with mouse digit 5 and delete it, I can't put any number insted it, because there is .22 which by stated rules does not allow any new insertions.

How should I change this code to allow insert numbers to left side of dot in such situation? Any suggestions?
Posted
Updated 6-May-13 9:26am
v2
Comments
Sergey Alexandrovich Kryukov 6-May-13 15:41pm    
Listen to a good friendly advice: never do it!
—SA

You almost never actually need rounding. Instead, you should use proper string formatting when you present numbers on scree. Please see:
http://msdn.microsoft.com/en-us/library/kfsatb94.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

By the way, it's also much safer then explicit rounding. What if you accidentally use the rounded value in calculations. It could lead to incorrect calculation result; and such bug would be hard to find.

—SA
 
Share this answer
 
Comments
Tautvydas Deržinskas 6-May-13 16:08pm    
The thing is that I am not presenting number to screen, I am checking how they are typed to textBox.
Sergey Alexandrovich Kryukov 6-May-13 16:21pm    
I got it. Don't do it. Nobody care how many digits it was, you should care only about the value. You need to use double.TryParse, it will validate for double and return double value in case of success. Then you should validate the value itself. Another option: use masked text box; but it may be not flexible enough. For some domains, this is the very it.
—SA
C#
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 :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900