The main feature of C1TextBox that distinguishes it from the standard TextBox control is that it works with typed data. When the user enters something in a C1TextBox control (or its descendant, C1DateEdit or C1NumericEdit), the input string undergoes several transition phases before it becomes a typed value of the Value property. The input string goes through the following transition phases:
The first phase is edit mask parsing (if an edit mask is active, see the Masked Input tutorial), extracting the stored content string out of the masked string displayed in the control.
The next phase is PreValidation of the input string. This validation is always performed over a string value, regardless of the DataType, since at this time the input string is not yet converted to a typed value (not yet parsed).
The next phase after pre-validation is parsing, that is, conversion to the required data type specified in the DataType property.
The last phase before modifying the Value property is PostValidation that is performed over the typed value obtained by parsing. If the value satisfies the PostValidation conditions, it is assigned to the Value property.
Of course, all these phases are optional, except parsing (and that is optional too, if DataType is set to String), they only occur if you specify validation conditions in corresponding properties.
This tutorial demonstrates pre-validation and post-validation, that is, how you can perform validation logic before and after parsing the input string.
Control | Property | Value |
---|---|---|
C1Label2 | TextDetached | True |
Text | Validation Before Parsing | |
C1Label3 | TextDetached | True |
Text | Country (Canada, France, Germany, UK, or USA): | |
C1Label4 | TextDetached | True |
Text | Phone Number: | |
C1Label5 | TextDetached | True |
Text | Fax: | |
C1Labe6 | TextDetached | True |
Text | Validation After Parsing | |
C1Label7 | TextDetached | True |
Text | Unit Price (from $0 to $10000, but not $12): | |
C1Label8 | TextDetached | True |
Text | Reorder Level: | |
Button1 | Name | btnClose |
Text | Close |
Control | Property | Value |
---|---|---|
C1ExpressTable1 | ConnectionComponent | C1ExpressConnection1 |
DbTableName | Customers | |
C1ExpressTable2 | ConnectionComponent | C1ExpressConnection1 |
DbTableName | Products |
Control | Property | Value |
---|---|---|
C1DbNavigator1 |
DataSource |
C1ExpressTable1 |
C1DbNavigator2 |
DataSource |
C1ExpressTable2 |
C1Label1 |
DataSource |
C1ExpressTable1 |
DataField |
CompanyName |
|
C1TextBox1 |
DataSource |
C1ExpressTable1 |
DataField |
Country |
|
C1TextBox2 |
DataSource |
C1ExpressTable1 |
DataField |
Phone |
|
C1TextBox3 |
DataSource |
C1ExpressTable1 |
DataField |
Fax |
|
C1TextBox4 |
DataSource |
C1ExpressTable2 |
DataField |
UnitPrice |
|
C1TextBox5 |
DataSource |
C1ExpressTable2 |
DataField |
UnitsInStock |
For the C1TextBox1 control:
The fact that PatternString represents an exact list of values to match the input string against is indicated by the value of the PreValidation.Validation property set to ExactList. This is default, so we do not have to change it.
Control | Property | Value |
---|---|---|
C1TextBox2 | PreValidation.Validation | Wildcards |
PreValidation.PatternString | (*) ###-####|##.##.##.##|####-###### |
Control | Property | Value |
---|---|---|
C1TextBox3 | PreValidation.Validation | RegexPattern |
PreValidation.PatternString | (\(\d+\) )?(\d+-\d+|(\d\d.){3}\d\d) |
The C1TextBox4 control validates the typed Value for the UnitPrice field (post-validation). The validation condition is that the price must be between 0 and 10,000 (0 and 10,000 included), and the price of $12.00 is not allowed. To specify such constraints:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TextBox5_PostValidating(ByVal sender As Object, ByVal e As C1.Win.C1Input.PostValidationEventArgs) Handles C1TextBox5.PostValidating If (CType(e.Value, Int16) < 0) Then e.ErrorInfo.ErrorMessage = "Value cannot be less than zero." ElseIf (CType(e.Value, Int16) > 5000) Then e.ErrorInfo.ErrorMessage = "Value cannot be greater than 5000." Else Return End If e.Succeeded = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void C1TextBox5_PostValidating(object sender, C1.Win.C1Input.PostValidationEventArgs e) { if ((Int16)e.Value < 0) e.ErrorInfo.ErrorMessage = "Value cannot be less than zero."; else if ((Int16)e.Value > 5000) e.ErrorInfo.ErrorMessage = "Value cannot be greater than 5000."; else return; e.Succeeded = false; } |
This code verifies that the value lies between 0 and 5000. In case of an error, it sets the e.ErrorMessage property that makes the specified message text appear in the error message after the event. Setting e.Suceeded to False indicates that the code has detected a validation error.
When any one of the validation conditions are not satisfied, an attempt to leave the control with incorrect value shows an error message box with caption "C1Input Validation Error" and a brief error description. In the C1TextBox5 control, the error message is one of the two specified in the code.