-
Notifications
You must be signed in to change notification settings - Fork 5k
/
MaskedTextBoxColumn.cs
331 lines (303 loc) · 11.5 KB
/
MaskedTextBoxColumn.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/********************************* Module Header **********************************\
* Module Name: MaskedTextBoxColumn.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
{
/// <summary>
/// The base object for the custom column type. Programmers manipulate
/// the column types most often when working with the DataGridView, and
/// this one sets the basics and Cell Template values controlling the
/// default behaviour for cells of this column type.
/// </summary>
public class MaskedTextBoxColumn : DataGridViewColumn
{
private string mask;
private char promptChar;
private bool includePrompt;
private bool includeLiterals;
private Type validatingType;
/// <summary>
/// Initializes a new instance of this class, making sure to pass
/// to its base constructor an instance of a MaskedTextBoxCell
/// class to use as the basic template.
/// </summary>
public MaskedTextBoxColumn()
: base(new MaskedTextBoxCell())
{
}
/// <summary>
/// Routine to convert from boolean to DataGridViewTriState.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static DataGridViewTriState TriBool(bool value)
{
return value ? DataGridViewTriState.True
: DataGridViewTriState.False;
}
/// <summary>
/// The template cell that will be used for this column by default,
/// unless a specific cell is set for a particular row.
///
/// A MaskedTextBoxCell cell which will serve as the template cell
/// for this column.
/// </summary>
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Only cell types that derive from MaskedTextBoxCell are supported
// as the cell template.
if (value != null && !value.GetType().IsAssignableFrom(
typeof(MaskedTextBoxCell)))
{
string s = "Cell type is not based upon the MaskedTextBoxCell.";
//CustomColumnMain.GetResourceManager().GetString("excNotMaskedTextBox");
throw new InvalidCastException(s);
}
base.CellTemplate = value;
}
}
/// <summary>
/// Indicates the Mask property that is used on the MaskedTextBox
/// for entering new data into cells of this type.
///
/// See the MaskedTextBox control documentation for more details.
/// </summary>
public virtual string Mask
{
get
{
return this.mask;
}
set
{
MaskedTextBoxCell mtbCell;
DataGridViewCell dgvCell;
int rowCount;
if (this.mask != value)
{
this.mask = value;
//
// First, update the value on the template cell.
//
mtbCell = (MaskedTextBoxCell)this.CellTemplate;
mtbCell.Mask = value;
//
// Now set it on all cells in other rows as well.
//
if (this.DataGridView != null && this.DataGridView.Rows != null)
{
rowCount = this.DataGridView.Rows.Count;
for (int x = 0; x < rowCount; x++)
{
dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
if (dgvCell is MaskedTextBoxCell)
{
mtbCell = (MaskedTextBoxCell)dgvCell;
mtbCell.Mask = value;
}
}
}
}
}
}
/// <summary>
/// By default, the MaskedTextBox uses the underscore (_) character
/// to prompt for required characters. This propertly lets you
/// choose a different one.
///
/// See the MaskedTextBox control documentation for more details.
/// </summary>
public virtual char PromptChar
{
get
{
return this.promptChar;
}
set
{
MaskedTextBoxCell mtbCell;
DataGridViewCell dgvCell;
int rowCount;
if (this.promptChar != value)
{
this.promptChar = value;
//
// First, update the value on the template cell.
//
mtbCell = (MaskedTextBoxCell)this.CellTemplate;
mtbCell.PromptChar = value;
//
// Now set it on all cells in other rows as well.
//
if (this.DataGridView != null && this.DataGridView.Rows != null)
{
rowCount = this.DataGridView.Rows.Count;
for (int x = 0; x < rowCount; x++)
{
dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
if (dgvCell is MaskedTextBoxCell)
{
mtbCell = (MaskedTextBoxCell)dgvCell;
mtbCell.PromptChar = value;
}
}
}
}
}
}
/// <summary>
/// Indicates whether any unfilled characters in the mask should be
/// be included as prompt characters when somebody asks for the text
/// of the MaskedTextBox for a particular cell programmatically.
///
/// See the MaskedTextBox control documentation for more details.
/// </summary>
public virtual bool IncludePrompt
{
get
{
return this.includePrompt;
}
set
{
MaskedTextBoxCell mtbc;
DataGridViewCell dgvc;
int rowCount;
if (this.includePrompt != value)
{
this.includePrompt = value;
//
// First, update the value on the template cell.
//
mtbc = (MaskedTextBoxCell)this.CellTemplate;
mtbc.IncludePrompt = TriBool(value);
//
// Now set it on all cells in other rows as well.
//
if (this.DataGridView != null && this.DataGridView.Rows != null)
{
rowCount = this.DataGridView.Rows.Count;
for (int x = 0; x < rowCount; x++)
{
dgvc = this.DataGridView.Rows.SharedRow(x).Cells[x];
if (dgvc is MaskedTextBoxCell)
{
mtbc = (MaskedTextBoxCell)dgvc;
mtbc.IncludePrompt = TriBool(value);
}
}
}
}
}
}
/// <summary>
/// Controls whether or not literal (non-prompt) characters should
/// be included in the output of the Text property for newly entered
/// data in a cell of this type.
///
/// See the MaskedTextBox control documentation for more details.
/// </summary>
public virtual bool IncludeLiterals
{
get
{
return this.includeLiterals;
}
set
{
MaskedTextBoxCell mtbCell;
DataGridViewCell dgvCell;
int rowCount;
if (this.includeLiterals != value)
{
this.includeLiterals = value;
//
// First, update the value on the template cell.
//
mtbCell = (MaskedTextBoxCell)this.CellTemplate;
mtbCell.IncludeLiterals = TriBool(value);
//
// Now set it on all cells in other rows as well.
//
if (this.DataGridView != null && this.DataGridView.Rows != null)
{
rowCount = this.DataGridView.Rows.Count;
for (int x = 0; x < rowCount; x++)
{
dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
if (dgvCell is MaskedTextBoxCell)
{
mtbCell = (MaskedTextBoxCell)dgvCell;
mtbCell.IncludeLiterals = TriBool(value);
}
}
}
}
}
}
/// <summary>
/// Indicates the type against any data entered in the MaskedTextBox
/// should be validated. The MaskedTextBox control will attempt to
/// instantiate this type and assign the value from the contents of
/// the text box. An error will occur if it fails to assign to this
/// type.
///
/// See the MaskedTextBox control documentation for more details.
/// </summary>
public virtual Type ValidatingType
{
get
{
return this.validatingType;
}
set
{
MaskedTextBoxCell mtbCell;
DataGridViewCell dgvCell;
int rowCount;
if (this.validatingType != value)
{
this.validatingType = value;
//
// First, update the value on the template cell.
//
mtbCell = (MaskedTextBoxCell)this.CellTemplate;
mtbCell.ValidatingType = value;
//
// Now set it on all cells in other rows as well.
//
if (this.DataGridView != null && this.DataGridView.Rows != null)
{
rowCount = this.DataGridView.Rows.Count;
for (int x = 0; x < rowCount; x++)
{
dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
if (dgvCell is MaskedTextBoxCell)
{
mtbCell = (MaskedTextBoxCell)dgvCell;
mtbCell.ValidatingType = value;
}
}
}
}
}
}
}
}