-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
MainForm.cs
244 lines (210 loc) · 6.83 KB
/
MainForm.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
/************************************* Module Header **************************************\
* Module Name: MaskedTextBoxColumn.cs
* Project: CSWinFormDataGridView
* Copyright (c) Microsoft Corporation.
*
* This sample demonstrates the use of custom column definitions within the Windows Forms
* DataGridView control.
*
* The Employee ID, SSN, State and Zip Code columns use MaskedTextBox controls for format
* and validate their input.
\******************************************************************************************/
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion
namespace CSWinFormDataGridView.CustomDataGridViewColumn
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
DataGridViewTextBoxColumn dgvTextBoxColumn;
MaskedTextBoxColumn mtbColumn;
//
// Employee name.
//
dgvTextBoxColumn = new DataGridViewTextBoxColumn();
dgvTextBoxColumn.HeaderText = "Name";
dgvTextBoxColumn.Width = 120;
this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
//
// Employee ID -- this will be of the format:
// [A-Z][0-9][0-9][0-9][0-9][0-9]
//
// this is well suited to using a MaskedTextBox column.
//
mtbColumn = new MaskedTextBoxColumn();
mtbColumn.HeaderText = "Employee ID";
mtbColumn.Mask = "L00000";
mtbColumn.Width = 75;
this.employeesDataGridView.Columns.Add(mtbColumn);
//
// [American] Social Security number, of the format:
// ###-##-####
//
mtbColumn = new MaskedTextBoxColumn();
mtbColumn.HeaderText = "SSN";
mtbColumn.Mask = "000-00-0000";
mtbColumn.Width = 75;
this.employeesDataGridView.Columns.Add(mtbColumn);
//
// Address
//
dgvTextBoxColumn = new DataGridViewTextBoxColumn();
dgvTextBoxColumn.HeaderText = "Address";
dgvTextBoxColumn.Width = 150;
this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
//
// City
//
dgvTextBoxColumn = new DataGridViewTextBoxColumn();
dgvTextBoxColumn.HeaderText = "City";
dgvTextBoxColumn.Width = 75;
this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
//
// State
//
mtbColumn = new MaskedTextBoxColumn();
mtbColumn.HeaderText = "State";
mtbColumn.Mask = "LL";
mtbColumn.Width = 40;
this.employeesDataGridView.Columns.Add(mtbColumn);
//
// Zip Code #####-#### (+4 optional)
//
mtbColumn = new MaskedTextBoxColumn();
mtbColumn.HeaderText = "Zip Code";
mtbColumn.Mask = "00000-0000";
mtbColumn.Width = 75;
mtbColumn.ValidatingType = typeof(ZipCode);
this.employeesDataGridView.Columns.Add(mtbColumn);
//
// Department Code
//
dgvTextBoxColumn = new DataGridViewTextBoxColumn();
dgvTextBoxColumn.HeaderText = "Department";
dgvTextBoxColumn.ValueType = typeof(int);
dgvTextBoxColumn.Width = 75;
this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
}
}
// Type that represents a US Zipcode to demonstrate
// the ValidatingType feature on the MaskedTextBox.
#region ZipCode Class
public class ZipCode
{
private int zipCode;
private int plusFour;
public ZipCode()
{
this.zipCode = 0;
this.plusFour = 0;
}
public ZipCode(string in_zipCode)
{
parseFromString(in_zipCode, out zipCode, out plusFour);
}
public ZipCode(int in_ivalue)
{
this.zipCode = in_ivalue;
this.plusFour = 0;
}
public static implicit operator ZipCode(string s)
{
return new ZipCode(s);
}
public static implicit operator ZipCode(int i)
{
return new ZipCode(i);
}
protected static void parseFromString
(
string in_string,
out int out_zipCode,
out int out_plusFour
)
{
int zc = 0, pf = 0;
char[] charray;
int x = 0;
if (in_string == null || in_string.Equals(""))
{
throw new ArgumentException("Invalid String");
}
charray = in_string.Trim().ToCharArray();
for (x = 0; x < 5; x++)
{
if (!Char.IsDigit(charray[x]))
{
throw new ArgumentException("Invalid String");
}
zc = zc * 10 + numfromchar(charray[x]);
}
while (x < charray.Length && !Char.IsDigit(charray[x]))
{
x++;
}
if (x < charray.Length)
{
for (int y = x; y < 4; y++)
{
if (!Char.IsDigit(charray[y]))
{
throw new ArgumentException("Invalid ZipCode String");
}
pf = pf * 10 + numfromchar(charray[y]);
}
}
out_zipCode = zc;
out_plusFour = pf;
}
private static int numfromchar(char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
throw new ArgumentException("invalid digit");
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder(10);
sb.Append(zipCode.ToString("00000"));
sb.Append("-");
sb.Append(plusFour.ToString("0000"));
return sb.ToString();
}
}
#endregion
}