Xceed Grid for WinForms v4.3 Documentation
Validation

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Editor Controls > Validation

Validation of the editors which have a TextBoxArea is done through the TextBoxArea's ValidatingText event. 

Validation of all other editors (as well as those that have a TextBoxArea) can be done through the Validating event as it would be done for other Windows Forms controls.

ValidatingText event

The ValidatingText event is raised prior to a modification of an editor's text, either through user interaction or direct affectation of the RawText, Text or Value properties.   It will also be raised when the focus is changed by using the keyboard (TAB, SHIFT+TAB, etc.), the mouse, by calling the Select, SelectNextControl, or Focus methods, or by setting the ContainerControl.ActiveControl property to the current form. 

The ValidatingText event will only be raised when the editor has passed its own built-in validation.   For most editors, this means that the ValidatingText event will only be raised when the text has passed its masking validation, depending on the value of the StrictValidation property. 

In addition to the basic built-in validation, the WinDatePicker control will also verify that the currently entered text is a parsable date and that it is within the minimum and maximum dates. In the case of the WinNumericTextBox control, its built-in validation will additionally verify that the current text is parsable into the datatype specified by the DataType property, and that the parsed value is within the minimum and maximum values. 

Text entered into the TextBoxArea will or will not be validated against the mask depending on the value of the StrictValidation property (by default, true). If the text passes this validation step, the ValidatingText event will be raised with e. FullValidation set to false.  Depending on the editor, the ValidatingText event might also be raised a second time with e.FullValidation set to true since the editor's Value property ( WinDatePicker, WinNumericTextBox ) is only synchronized if the current text passes the full validation process. 

When setting the Text, RawText, or Value properties, or when the control looses focus, the built-in validation will be "full" meaning that the mask will be validated regardless of the value of the StrictValidation property, and all of the editor specific validation will execute (validation against the editor's MinValue and MaxValue properties, etc.).

See example

VB.NET
Copy Code
' Prevent the user from entering "Hello World"

Private Sub TextBoxArea_ValidatingText( ByVal sender As Object, ByVal e As ValidatingTextEventArgs )

  If e.EditText = "Hello World" Then

    e.Valid = False

  End If

End Sub

' Allow "Hello World" to be entered, but prevent the user from leaving edit mode if it is.

Private Sub TextBoxArea_ValidatingText( ByVal sender As Object, ByVal e As ValidatingTextEventArgs )

  If e.FullValidation And e.EditText = "Hello World" Then

    e.Valid = False

  End If

End Sub

Dim datePicker As New WinDatePicker()

AddHandler datePicker.TextBoxArea.ValidatingText, AddressOf Me.DatePicker_Validation     

Me.Controls.Add( datePicker )

' Allow "2005,1,1" to be entered, but prevent the user from leaving edit mode if it is.

'

' In this case, the Value property will not contain "2005,1,1" but rather the last valid value.

' If the same validation would have been done in the control's Validating event rather than the

' ValidatingText event, the Value property would have contained "2005,1,1" rather than the last

' valid value.

Private Sub DatePicker_Validation( ByVal sender As Object, ByVal e As ValidatingTextEventArgs )

  If e.FullValidation Then

    Dim parent As WinDatePicker = CType( CType( sender, DatePickerTextBoxArea ).Parent, _

                                         WinDatePicker )

    Dim parsedDateTime As DateTime

    parseSuccessful As Boolean = parent.TryParse(e.EditText, parsedDateTime)

    If( Not parseSuccessful ) Or ( parsedDateTime = New DateTime( 2005,1,1 ) ) Then

      e.Valid = False

    End If

  End If

End Sub
C#
Copy Code
// Prevent the user from entering "Hello World"

private void TextBoxArea_ValidatingText( object sender, ValidatingTextEventArgs e )

{

  if( e.EditText == "Hello World" )

  {

    e.Valid = false;

  }

}

// Allow "Hello World" to be entered, but prevent the user from leaving edit mode if it is.

private void TextBoxArea_ValidatingText( object sender, ValidatingTextEventArgs e )

{

  if( ( e.FullValidation ) && ( e.EditText == "Hello World" ) )

  {

    e.Valid = false;

  }

}

WinDatePicker datePicker = new WinDatePicker();

datePicker.TextBoxArea.ValidatingText += new System.ComponentModel.CancelEventHandler( this.DatePicker_Validation );     

this.Controls.Add( datePicker );

// Allow "2005,1,1" to be entered, but prevent the user from leaving edit mode if it is.

//

// In this case, the Value property will not contain "2005,1,1" but rather the last valid value.

// If the same validation would have been done in the control's Validating event rather than the

// ValidatingText event, the Value property would have contained "2005,1,1" rather than the last

// valid value.

private void DatePicker_Validation( object sender, ValidatingTextEventArgs e )

{

  if( e.FullValidation )

  {

    WinDatePicker parent = ( WinDatePicker )( ( DatePickerTextBoxArea )sender ).Parent;

    DateTime parsedDateTime;

    bool parseSuccessful = parent.TryParse( e.EditText, out parsedDateTime );

    if( ( !parseSuccessful ) || ( parsedDateTime == new DateTime( 2005,1,1)   ) )

    {

      e.Valid = false;

    }

  }

}
If an exception is thrown in during the ValidatingText event, the exception will be caught and the validation of the text will be perceived as invalid, resulting in the same behavior as if you would have set e.Valid to false.

ValidateText method

The TextBoxAreas of the WinTextBox, WinComboBox, WinNumericTextBox, and WinDatePicker controls provide the ValidateText method which can be used to validate a string against a Mask. The specified text must be passed as though the mask were applied to it. For example, if ">@9@-9@9" is used as the mask, then the ValidateText method would return true for "J4L-4C3", but false for "J4L4C3". In the case of the WinDatePicker control, the ValidateText method will validate the string against the DisplayFormatSpecifier and/or EditFormatSpecifier properties.

Input Validator control

The Input Validator control provides an easy-to-use, self-wiring text validation component, called ValidationProvider. The component can be dragged and dropped onto any Windows Form, which adds a validation expression property representing a validation criterion, to each compatible input component. Each criterion can then be configured using either the Validation Configuration dialog or the Property window, allowing developers to quickly set up validation at design time. Input components are then automatically validated at runtime. The validation component also features fully customizable error messages and a regular expression tester. 

One of the greatest strengths of the Input Validator control is the fact that it can be used to add flexible validation to input components without having to resort to coding it. Validation criteria can be configured using simple dialog boxes. 

For more information on the Input Validator control, please refer to the Input Validator control documentation.