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
/
CustomEdit.cs
134 lines (118 loc) · 4.78 KB
/
CustomEdit.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Popup;
using System.Globalization;
using DevExpress.Data.Mask;
namespace T223817 {
[UserRepositoryItem("RegisterCustomEdit")]
public class RepositoryItemCustomEdit : RepositoryItemTextEdit {
private static readonly object invalidCharacter = new object();
static RepositoryItemCustomEdit() {
RegisterCustomEdit();
}
public const string CustomEditName = "CustomEdit";
public RepositoryItemCustomEdit() {
}
public override string EditorTypeName {
get { return CustomEditName; }
}
public static void RegisterCustomEdit() {
Image img = null;
EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(CustomEdit), typeof(RepositoryItemCustomEdit), typeof(TextEditViewInfo), new TextEditPainter(), true, img));
}
public override void Assign(RepositoryItem item) {
RepositoryItemCustomEdit source = item as RepositoryItemCustomEdit;
BeginUpdate();
try {
base.Assign(item);
if (source == null) {
return;
}
}
finally {
EndUpdate();
}
Events.AddHandler(invalidCharacter, source.Events[invalidCharacter]);
}
public event InvalidCharacterEventHandler InvalidCharacter {
add { this.Events.AddHandler(invalidCharacter, value); }
remove { this.Events.RemoveHandler(invalidCharacter, value); }
}
protected internal virtual void RaiseInvalidCharacter(InvalidCharacterEventArgs e) {
InvalidCharacterEventHandler handler = (InvalidCharacterEventHandler)this.Events[invalidCharacter];
if (handler != null) {
handler(GetEventSender(), e);
}
}
}
public delegate void InvalidCharacterEventHandler(object sender, InvalidCharacterEventArgs e);
public class InvalidCharacterEventArgs : EventArgs {
public string InsertionString { get; private set; }
public bool Handled { get; set; }
public InvalidCharacterEventArgs(string insertionString) {
InsertionString = insertionString;
}
}
[ToolboxItem(true)]
public class CustomEdit : TextEdit {
static CustomEdit() {
RepositoryItemCustomEdit.RegisterCustomEdit();
}
public CustomEdit() {
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public new RepositoryItemCustomEdit Properties {
get { return base.Properties as RepositoryItemCustomEdit; }
}
public override string EditorTypeName {
get { return RepositoryItemCustomEdit.CustomEditName; }
}
private NumericMaskManager fManager;
protected override MaskManager CreateMaskManager(DevExpress.XtraEditors.Mask.MaskProperties mask) {
fManager = (NumericMaskManager)base.CreateMaskManager(mask);
return fManager;
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) {
base.OnKeyPress(e);
string insertion = KeyCharToInsertableString(e.KeyChar);
if (insertion == null) {
return;
}
if (fManager.Insert(insertion)) {
fManager.Backspace();
return;
}
ProcessInvalidCharacter(insertion);
}
private void ProcessInvalidCharacter(string insertion) {
InvalidCharacterEventArgs args = new InvalidCharacterEventArgs(insertion);
this.Properties.RaiseInvalidCharacter(args);
if (args.Handled) {
return;
}
this.ToolTipController.ShowBeak = true;
this.ToolTipController.ShowHint("An editor can accept only numbers or maximum length achieved", this.PointToScreen(Point.Empty));
}
private string KeyCharToInsertableString(char keyChar) {
if (!char.IsControl(keyChar)) {
return keyChar.ToString(CultureInfo.InvariantCulture);
}
if (keyChar == '\r' && this.MaskBox.Multiline && this.MaskBox.AcceptsReturn) {
return "\r\n";
}
if (keyChar == '\t' && this.MaskBox.Multiline && this.MaskBox.AcceptsTab) {
return "\t";
}
return null;
}
}
}