This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdvancedDXValidationProvider.cs
68 lines (63 loc) · 2.46 KB
/
AdvancedDXValidationProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.DXErrorProvider;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using DevExpress.XtraEditors;
namespace CustomValidationProvider {
[System.ComponentModel.DesignerCategory("")]
public class AdvancedDXValidationProvider : DXValidationProvider {
Dictionary<Control, ValidationRuleBase> hashTable = new Dictionary<Control, ValidationRuleBase>();
ErrorProvider msErrorProvider = new ErrorProvider();
public AdvancedDXValidationProvider() {
}
public AdvancedDXValidationProvider(IContainer container)
: base(container) {
}
public AdvancedDXValidationProvider(ContainerControl parentControl)
: base(parentControl) {
}
private void RemoveFromHash(Control control) {
if (hashTable.ContainsKey(control))
hashTable.Remove(control);
}
public override void SetValidationRule(Control control, ValidationRuleBase rule) {
if (control is TextBoxBase) {
RemoveFromHash(control);
if (rule != null) {
hashTable.Add(control, rule);
SubscribeValidating(control);
}
else
UnsubscribeValidating(control);
}
}
void SubscribeValidating(Control control) {
control.Validating += control_Validating;
}
void UnsubscribeValidating(Control control) {
control.Validating -= control_Validating;
}
void control_Validating(object sender, CancelEventArgs e) {
TextBoxBase control = sender as TextBoxBase;
msErrorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
msErrorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
if (!Validate(control)) {
msErrorProvider.SetError(control, "Please enter a valid value");
e.Cancel = true;
}
else {
msErrorProvider.SetError(control, "");
}
}
public bool Validate(Control control) {
TextBoxBase ctrl = control as TextBoxBase;
ValidationRuleBase rule = hashTable[control];
if (rule == null || !rule.CanValidate(ctrl)) return true;
return rule.Validate(ctrl, ctrl.Text);
}
}
}