Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys!
I would like to know if there is any way to accept only numbers with two decimal places (example: 123.55) only on my text boxes. I have not yet tried any code but what I have searched is using the masked text box. But I do not like to use it due to the fact that I do not know about it :) anyone can help me? Thanks in advance!
Posted
Comments
BillWoodruff 20-Dec-14 0:54am    
"masked text box. But I do not like to use it due to the fact that I do not know about it"

So you are lazy, and we are supposed to take care of you ?
DarkDreamer08 20-Dec-14 2:15am    
it is not that i am being lazy. i am just used to use text box.

1 solution

Writre code textbox keypress event like Below

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
    {
            e.Handled = true;
    }

    // only allow one decimal point
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
}
 
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