Back to Devexpress

How to: Restrict Values Being Entered in Editors

windowsforms-9455-controls-and-libraries-editors-and-simple-controls-examples-how-to-restrict-values-being-entered-in-editors.md

latest1.5 KB
Original Source

How to: Restrict Values Being Entered in Editors

  • Oct 25, 2019

The following sample code prohibits values greater than 100 being entered into the spin editor. The RepositoryItem.Validating event is handled to check the entered value’s validity. The BaseEdit.InvalidValue event is handled to display an exception hint if an invalid value has been entered.

csharp
using System.ComponentModel;
using DevExpress.XtraEditors.Controls;

private void spinEdit1_Validating(object sender, CancelEventArgs e) {
   if((sender as SpinEdit).Value >= 100)
      e.Cancel = true;
}
private void spinEdit1_InvalidValue(object sender, InvalidValueExceptionEventArgs e) {
   e.ErrorText = "The value should be less than 100";
}
vb
Imports DevExpress.XtraEditors.Controls

Private Sub SpinEdit1_Validating(ByVal sender As Object, _
  ByVal e As System.ComponentModel.CancelEventArgs) _
  Handles SpinEdit1.Validating
   If (TryCast(sender, SpinEdit)).Value >= 100 Then
      e.Cancel = True
   End If
End Sub

Private Sub SpinEdit1_InvalidValue(ByVal sender As Object, _
  ByVal e As DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs) _
  Handles SpinEdit1.InvalidValue
    e.ErrorText = "The value should be less than 100"
End Sub