Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have question like this

in a c# win application it's contain text box called txtQty it has block to enter characters by using following code

C#
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }


but user can enter value like this (5...) this input generate an error how to block this

thanks
Posted
Comments
MT_ 6-Dec-12 6:50am    
Its not clear what all characters you want to allow ? Your code above is allowing 5... ? Which event you have written this code?

Hi,

Please have a look at the following:

Working with the MaskedTextBox Control[^]
Masked C# TextBox Control[^]

Kind regards,
 
Share this answer
 
You can also check for double.tryparse inside your code since you want to accept one decimal place.
C#
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
  double dblValue;
  e.Handled = double.tryparse(txtQty.Text,out dblValue);
}

this ensures that the value entred in the text box is a valid decimal number and protects from additional ....( dots :))
 
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