TextEdit验证

  1 using System;

  2 using System.Collections.Generic;

  3 using System.ComponentModel;

  4 using System.Drawing;

  5 using System.Data;

  6 using System.Text;

  7 using System.Windows.Forms;

  8 using DevExpress.XtraEditors.DXErrorProvider;

  9 

 10 namespace DevExpress.Tutorials.XtraEditors {

 11     public partial class ModuleDXValidationProvider : DevExpress.Tutorials.XtraEditors.Tutorial {

 12         public ModuleDXValidationProvider() {

 13             InitializeComponent();

 14             InitValidationRules();

 15             InitFieldValues();

 16         }

 17         private void ModuleDXValidationProvider_Load(object sender, EventArgs e) {

 18             manualValidate.Checked = true;

 19             dxValidationProvider1.ValidationMode = ValidationMode.Manual;

 20             dxValidationProvider1.Validate();

 21         }

 22         // <autoValidateCheck>

 23         // <manualValidate>

 24         private void OnCheckedChanged(object sender, EventArgs e) {

 25             dxValidationProvider1.ValidationMode = (manualValidate.Checked ? ValidationMode.Manual : ValidationMode.Auto);

 26             btnValidate.Enabled = manualValidate.Checked;

 27         }

 28         // </manualValidate>

 29         // </autoValidateCheck>

 30 

 31         private void InitValidationRules() {

 32             // <notEmptyTextEdit> 

 33             ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();

 34             notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;

 35             notEmptyValidationRule.ErrorText = "Please enter a value";

 36             // </notEmptyTextEdit>

 37 

 38             // <rangeTextEdit>

 39             ConditionValidationRule rangeValidationRule = new ConditionValidationRule();

 40             rangeValidationRule.ConditionOperator = ConditionOperator.Between;

 41             rangeValidationRule.Value1 = 0;

 42             rangeValidationRule.Value2 = 100;

 43             rangeValidationRule.ErrorText = "Please enter a value between 1 and 100";

 44             // </rangeTextEdit>

 45 

 46             // <notEqualsComboBoxEdit>

 47             ConditionValidationRule notEqualsValidationRule = new ConditionValidationRule();

 48             notEqualsValidationRule.ConditionOperator = ConditionOperator.NotEquals;

 49             notEqualsValidationRule.Value1 = "(select)";

 50             notEqualsValidationRule.ErrorText = "Please choose a value";

 51             notEqualsValidationRule.ErrorType = ErrorType.Information;

 52             // </notEqualsComboBoxEdit>

 53 

 54             // <containsTextEdit>

 55             ConditionValidationRule containsValidationRule = new ConditionValidationRule();

 56             containsValidationRule.ConditionOperator = ConditionOperator.Contains;

 57             containsValidationRule.Value1 = '@';

 58             containsValidationRule.ErrorText = "Please enter a valid email";

 59             containsValidationRule.ErrorType = ErrorType.Warning;

 60             // </containsTextEdit>

 61 

 62             // <compareTextEdit>

 63             CompareAgainstControlValidationRule compareValidationRule = new CompareAgainstControlValidationRule();

 64             compareValidationRule.Control = notEmptyTextEdit;

 65             compareValidationRule.CompareControlOperator = CompareControlOperator.Equals;

 66             compareValidationRule.ErrorText = "Please enter a value that equals to the first editor's value";

 67             compareValidationRule.CaseSensitive = true;

 68             // </compareTextEdit>

 69             

 70             // <customTextEdit>

 71             CustomValidationRule customValidationRule = new CustomValidationRule();

 72             customValidationRule.ErrorText = "Please enter a valid person name";

 73             customValidationRule.ErrorType = ErrorType.Warning;

 74             // </customTextEdit>

 75             

 76             // <notEmptyTextEdit>

 77             dxValidationProvider1.SetValidationRule(notEmptyTextEdit, notEmptyValidationRule);

 78             // </notEmptyTextEdit>

 79             // <rangeTextEdit>

 80             dxValidationProvider1.SetValidationRule(rangeTextEdit, rangeValidationRule);

 81             // </rangeTextEdit>

 82             // <containsTextEdit>

 83             dxValidationProvider1.SetValidationRule(containsTextEdit, containsValidationRule);

 84             // </containsTextEdit>

 85             // <notEqualsComboBoxEdit>

 86             dxValidationProvider1.SetValidationRule(notEqualsComboBoxEdit, notEqualsValidationRule);

 87             // </notEqualsComboBoxEdit>

 88             dxValidationProvider1.SetValidationRule(compareTextEdit, compareValidationRule);

 89             // <customTextEdit>

 90             dxValidationProvider1.SetValidationRule(customTextEdit, customValidationRule);

 91             // </customTextEdit>

 92         }

 93         private void InitFieldValues() {

 94             notEmptyTextEdit.EditValue = "DevExpress";

 95             rangeTextEdit.EditValue = 570;

 96             containsTextEdit.EditValue = "[email protected]";

 97             notEqualsComboBoxEdit.EditValue = "(select)";

 98             compareTextEdit.EditValue = notEmptyTextEdit.EditValue;

 99             customTextEdit.EditValue = "Mr. Smith";  

100         }

101         // <manualValidate>

102         // <btnValidate>

103         private void btnValidate_Click(object sender, EventArgs e) {

104             dxValidationProvider1.Validate();

105         }

106         // </btnValidate>

107         // </manualValidate>

108 

109         #region CustomValidationRule

110         // <customTextEdit>

111         public class CustomValidationRule : ValidationRule {

112             public override bool Validate(Control control, object value) {

113                 string str = (string)value;

114                 string[] values = new string[] { "Dr.", "Mr.", "Mrs.", "Miss", "Ms." };

115                 bool res = false;

116                 foreach(string val in values) {

117                     if(ValidationHelper.Validate(str, ConditionOperator.BeginsWith, val, null, null, false)) {

118                         string name = str.Substring(val.Length);

119                         if(name.Trim().Length > 0) res = true;

120                     }

121                 }

122                 return res;

123             }

124         }

125         // </customTextEdit>

126         #endregion 

127     }

128 }

 

你可能感兴趣的:(text)