views:

17

answers:

3

My requirment is i want to enter decimal value in my textbox ,In case if the user entered the .dot he must enter 3 decimal value other wise it should not allow to leave textbox.if te user not entered .dot he may allow to enter any no of digit how to restrict this in .net winforms textbox.

decimal((1-9),3)

Before the decimal place it may be any decimal value like

True case

123--- 1232.004--- 123123123123.555--- 3123123123--- .231---

False case

123.--- 222.33--- 123.3--- 21312323.1232133--- .2--- .12313---

+1  A: 

Subscribe to TextBox Validating event:

private void TextBox_Validating(object sender, CancelEventArgs e)
{
    string Text = ((TextBox)sender).Text;        
    string[] Parts = Text.Split(new char[] { '.' });
    decimal Dummy;
    e.Cancel = !decimal.TryParse(Text, out Dummy) || 
        ((Parts.Length == 2) && (Parts[1].Length != 3));
}

Or you can use Regular Expressions for text validating or something else. But main idea is here if you cancelling validation on text box then user cannot leave it by default. Check also AutoValidate property.

arbiter
+1  A: 

Just handle either the Validating event and check for ., if you find any make sure it's followed by 3 digits.

Just use IndexOf to find the dots, check that the string contains at least 3 more characters after that index and then get out the next 3 characters and make sure they're all digits.

ho1
A: 

Or break the entry into two textboxes, one for digits before the dot, the second for the three digits after the dot.

Kind of like entering a SSN with 3 text boxes.

Beth