我在 .NET 和 DevExpress 3rd Party Components 中使用 WinForms。如何验证一个表单中多个组件的数据并以自定义的方式对其进行汇总?
问问题
5498 次
1 回答
1
试试这个代码:
using DevExpress.XtraEditors.DXErrorProvider;
// ...
ConditionValidationRule containsValidationRule = new ConditionValidationRule();
containsValidationRule.ConditionOperator = ConditionOperator.Contains;
containsValidationRule.Value1 = '@';
containsValidationRule.ErrorText = "Please enter a valid email";
containsValidationRule.ErrorType = ErrorType.Warning;
CompareAgainstControlValidationRule compValidationRule =
new CompareAgainstControlValidationRule();
compValidationRule.Control = notEmptyTextEdit;
compValidationRule.CompareControlOperator = CompareControlOperator.Equals;
compValidationRule.ErrorText = "Please enter a value that equals to the first editor's value";
compValidationRule.CaseSensitive = true;
CustomValidationRule customValidationRule = new CustomValidationRule();
customValidationRule.ErrorText = "Please enter a valid person name";
customValidationRule.ErrorType = ErrorType.Warning;
dxValidationProvider1.SetValidationRule(containsTextEdit, containsValidationRule);
dxValidationProvider1.SetValidationRule(compareTextEdit, compValidationRule);
dxValidationProvider1.SetValidationRule(customTextEdit, customValidationRule);
// Represents a custom validation rule.
public class CustomValidationRule : ValidationRule {
public override bool Validate(Control control, object value) {
string str = (string)value;
string[] values = new string[] { "Dr.", "Mr.", "Mrs.", "Miss", "Ms." };
bool res = false;
foreach(string val in values) {
if(ValidationHelper.Validate(str, ConditionOperator.BeginsWith,
val, null, null, false)) {
string name = str.Substring(val.Length);
if(name.Trim().Length > 0) res = true;
}
}
return res;
}
}
来源:http ://documentation.devexpress.com/#WindowsForms/CustomDocument9481
于 2012-09-06T10:35:57.057 回答