C validating text box
The masking language that is used by Masked Text Box is very flexible.It allows you to specify required characters, optional characters, literal characters, such as hyphens and parentheses, currency characters, and date separators.Validation is very useful when you have bound your controls to a data source, such as a database table.By using validation, you can make sure that your control's data satisfies the format required by the data source, and that it does not contain any special characters such as quotation marks and back slashes that might be unsafe.Thanks, Alpha One of the easiest ways would be to call a function like this from your event handler that is handling the Validating event: private bool Is Int(Text Box textbox) To call Is Int, you pass in a reference to the Text Box in question and it attempts to parse the value in the Text property as an integer, if it succeeds (ie doesn’t raise an exception), it returns true.If it fails however (ie it was unable to parse it as an int), it will raise an exception internally and return false.If you assigned Enable Prevent Focus Change, canceling the event will cause the Validated event not to occur.Input focus will remain on the current control until the user changes the data to a valid input.
I want to make sure that the users enter integer numbers before I save it to the database.There are other ways you could do this (ie not involving raising of exceptions willy nilly), like making sure that the only numeric characters are in the string, or using Regular Expressions...however as I said, this is really the quickest and easiest way (code wise) to do it.You can validate the data as the data is entered in a control by reading the keys as they are pressed, or more commonly whenever the user takes the input focus away from one control and moves to the next.
This approach is useful when you want to give the user immediate feedback about the data as they are working.
If you need to require users to enter data in a well-defined format, such as a telephone number or a part number, you can accomplish this quickly and with minimal code by using the Masked Text Box control.