MaskedTextbox to accept only doubles
Hi, I added a few MaskedTextboxes to try to control user input, only to find a few more problems!
In a normal Textbox, whatever text the user inputs is the only input, but in a MaskedTextbox, the mask remains when validation starts. The problem is, spaces or invalid characters might remain and parsing for a double fails. Example:
Code:
Using:
Mask = "00.0000"
ValidationType = typeof(double)
Convert.ToDouble(maskedTextbox.Text)
Input after selecting all text in the box:
Code:
Using:
"1" --> Upon validation is parsed as "1_.____" --> which means it's "1 ." --> crash!
"01" --> Upon validation is parsed as "01.____" --> which ends with "." --> crash!
"010" --> Upon validation is parsed as "01.0___" --> OK.
For the first two, you get a formatting exception. Will I have to parse the whole string to check everything? Isn't that the point of having a MaskedTextbox?? I can't be asking the user to type extra zeros!
Should I go with a regular Textbox and use double.TryParse() ?
Also, I've noticed there is no exclusive mask for plus/minus symbols. I can only use "&" and that would allow the user to input an extra number. That's an easy check, but is there a shorter/better way?
Re: MaskedTextbox to accept only doubles
Greetings...
On you masked edit box, check the property "TextMaskFormat", play around with the other properties also... PromptChar, AllowPromptAsInput, etc...
Best of luck!