Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want that to forms textBox2 would be possible to type only values in such format:
HTML
000.00
87979845.00

I mean unlimited digit number, then decimal/dot and only two numbers after it. So far I have everything except check of how many numbers after the dot is...
C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && !textBox2.Text.Contains(".") || e.KeyChar == '\b')
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}

How I could implement that in this code?
Posted
Updated 5-May-13 3:32am
v4

Modify your function like below...
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;
    }
}

Let me know if it works or not...
 
Share this answer
 
v2
Comments
Tautvydas Deržinskas 5-May-13 8:23am    
It works, thank You.
Hi @Tautvydas Deržinskas,

Great news... Thanks for accepting the answer.
Most welcome. Anytime.

Happy Coding.

Thanks,
Tadit.
Tautvydas Deržinskas 5-May-13 9:58am    
Hey, I found one more problem. When someone enters number ex. 35.55 then deletes 35 (.35 left in textbox) it's not possible to add anything before dot.. Can it be fixed?
Maciej Los 5-May-13 12:14pm    
Please, see my answer ;)
Ok, let me see and try to fix this...
I will reply you. Thanks for the test.
You did not provide enough information... Some countries uses different format for decimal values, for example:
English: 100.23
Polish: 100,23

Do you know what i mean, now?

Please, refer below links:
http://msdn.microsoft.com/en-us/library/aa292205%28v=vs.71%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx[^]

I think you are looking for numeric validator. Please, see past answer: allow Only numeric Characters into Textbox[^]

But if you want to add new functionality to TextBox and use it as MaskedTextBox, please, refer these:
ContentStringFormat in wpf[^]
How can I validate user entered string in textbox?[^]
showing masked nput into Textbox[^]

CP article:
Numeric TextBox : Allow your users to enter numeric data the easy way[^]
 
Share this answer
 
v2

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