Click here to Skip to main content
15,918,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box in which user enters hours like ex: 10.5 or 2.5 , so when the user just enters 10 or 2, then the text box will display ".0" as appended to 10 or 2.
and finally the text box will displayed as value as 10.0 or 2.0.

So the maximum lenghtof textBox is 4. so when the user enters values like 1111, the textbox will be appended with ".0". How to restrict the lenght of textbox so that it will accept only values like ex: 1.2 or 10.5 or 12.0 or 1.0. and not to accept values like 1111.0

am using the follwoing code to code to append ".0"

C#
private void txtPlannedHours_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            txtPlannedHours.Text = Convert.ToDouble(txtPlannedHours.Text).ToString("0.0");
        }


And am using the following regular expression:
Regex re = new Regex("^\\d{1,2}([.]\\d{1})$");

can anyone please suggest me?
Posted
Updated 24-Feb-14 19:20pm
v2
Comments
Kannan Subramaniam 25-Feb-14 1:57am    
For restricting the Text length. Set MaxLength property of TextBox.
Member 10593922 27-Feb-14 1:15am    
yes i have set the MaxLenght property as 4 in wpf(Xaml).But still when the user enter values like 1111, it will append .0 because of the txtPlannedHours_LostKeyboardFocus event. so i need to include the above regular expression as well as it has to append ".0" when values are entered without the decimal point. so please provide me a solution so that i can merge both the condition

 
Share this answer
 
v2
Comments
Member 10593922 25-Feb-14 7:24am    
thank u.. i will check and try to implement
First of all, this is not the best way to right code in WPF.
I agree this works but what you should be using is Data Binding.

Display this data in the UI by using data binding.
I will leave you to explore more about data binding - Data Binding (WPF)[^].

Once you bind data, you can validate data using Validation rules for e.g. http://msdn.microsoft.com/en-us/library/ms752347%28v=vs.110%29.aspx#data_validation[^].
 
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