-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
MaskedTextBoxEditingControl.cs
257 lines (227 loc) · 8.24 KB
/
MaskedTextBoxEditingControl.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/********************************* Module Header **********************************\
* Module Name: MaskedTextBoxEditingControl.cs
* Project: CSWinFormDataGridView
* Copyright (c) Microsoft Corporation.
*
* This sample demonstrates how to create a custom DataGridView column.
\**********************************************************************************/
#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion
namespace CSWinFormDataGridView.CustomDataGridViewColumn
{
public class MaskedTextBoxEditingControl : MaskedTextBox, IDataGridViewEditingControl
{
protected int rowIndex;
protected DataGridView dataGridView;
protected bool valueChanged = false;
public MaskedTextBoxEditingControl()
{
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
// Let the DataGridView know about the value change
NotifyDataGridViewOfValueChange();
}
/// <summary>
/// Notify DataGridView that the value has changed.
/// </summary>
protected virtual void NotifyDataGridViewOfValueChange()
{
this.valueChanged = true;
if (this.dataGridView != null)
{
this.dataGridView.NotifyCurrentCellDirty(true);
}
}
#region IDataGridViewEditingControl Members
// Indicates the cursor that should be shown when the user hovers their
// mouse over this cell when the editing control is shown.
public Cursor EditingPanelCursor
{
get
{
return Cursors.IBeam;
}
}
// Returns or sets the parent DataGridView.
public DataGridView EditingControlDataGridView
{
get
{
return this.dataGridView;
}
set
{
this.dataGridView = value;
}
}
// Sets/Gets the formatted value contents of this cell.
public object EditingControlFormattedValue
{
set
{
this.Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
get
{
return this.Text;
}
}
// Get the value of the editing control for formatting.
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Text;
}
// Process input key and determine if the key should be used for the editing control
// or allowed to be processed by the grid. Handle cursor movement keys for the MaskedTextBox
// control; otherwise if the DataGridView doesn't want the input key then let the editing control handle it.
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Right:
//
// If the end of the selection is at the end of the string
// let the DataGridView treat the key message
//
if (!(this.SelectionLength == 0
&& this.SelectionStart == this.ToString().Length))
{
return true;
}
break;
case Keys.Left:
//
// If the end of the selection is at the begining of the
// string or if the entire text is selected send this character
// to the dataGridView; else process the key event.
//
if (!(this.SelectionLength == 0
&& this.SelectionStart == 0))
{
return true;
}
break;
case Keys.Home:
case Keys.End:
if (this.SelectionLength != this.ToString().Length)
{
return true;
}
break;
case Keys.Prior:
case Keys.Next:
if (this.valueChanged)
{
return true;
}
break;
case Keys.Delete:
if (this.SelectionLength > 0 || this.SelectionStart < this.ToString().Length)
{
return true;
}
break;
}
//
// defer to the DataGridView and see if it wants it.
//
return !dataGridViewWantsInputKey;
}
// Prepare the editing control for edit.
public void PrepareEditingControlForEdit(bool selectAll)
{
if (selectAll)
{
SelectAll();
}
else
{
//
// Do not select all the text, but position the caret at the
// end of the text.
//
this.SelectionStart = this.ToString().Length;
}
}
// Indicates whether or not the parent DataGridView control should
// reposition the editing control every time value change is indicated.
// There is no need to do this for the MaskedTextBox.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Indicates the row index of this cell. This is often -1 for the
// template cell, but for other cells, might actually have a value
// greater than or equal to zero.
public int EditingControlRowIndex
{
get
{
return this.rowIndex;
}
set
{
this.rowIndex = value;
}
}
// Make the MaskedTextBox control match the style and colors of
// the host DataGridView control and other editing controls
// before showing the editing control.
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
}
// Gets or sets our flag indicating whether the value has changed.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
this.valueChanged = value;
}
}
#endregion // IDataGridViewEditingControl.
/// <summary>
/// Routine to translate between DataGridView content alignments and text
/// box horizontal alignments.
/// </summary>
/// <param name="align"></param>
/// <returns></returns>
private static HorizontalAlignment translateAlignment(DataGridViewContentAlignment align)
{
switch (align)
{
case DataGridViewContentAlignment.TopLeft:
case DataGridViewContentAlignment.MiddleLeft:
case DataGridViewContentAlignment.BottomLeft:
return HorizontalAlignment.Left;
case DataGridViewContentAlignment.TopCenter:
case DataGridViewContentAlignment.MiddleCenter:
case DataGridViewContentAlignment.BottomCenter:
return HorizontalAlignment.Center;
case DataGridViewContentAlignment.TopRight:
case DataGridViewContentAlignment.MiddleRight:
case DataGridViewContentAlignment.BottomRight:
return HorizontalAlignment.Right;
}
throw new ArgumentException("Error: Invalid Content Alignment!");
}
}
}