PageRenderTime 43ms CodeModel.GetById 31ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormDataGridView/CustomDataGridViewColumn/MainForm.cs

#
C# | 256 lines | 169 code | 39 blank | 48 comment | 11 complexity | 0ad889bfb1991b0ff933717db2c32b72 MD5 | raw file
  1/************************************* Module Header **************************************\
  2* Module Name:	MaskedTextBoxColumn.cs
  3* Project:		CSWinFormDataGridView
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* This sample demonstrates the use of custom column definitions within the Windows Forms 
  7* DataGridView control.
  8* 
  9* The Employee ID, SSN, State and Zip Code columns use MaskedTextBox controls for format 
 10* and validate their input.
 11* 
 12* This source is subject to the Microsoft Public License.
 13* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 14* All other rights reserved.
 15* 
 16* History:
 17* * 6/05/2009 3:00 PM Zhi-Xin Ye Created
 18\******************************************************************************************/
 19
 20#region Using directives
 21using System;
 22using System.Collections.Generic;
 23using System.ComponentModel;
 24using System.Data;
 25using System.Drawing;
 26using System.Linq;
 27using System.Text;
 28using System.Windows.Forms;
 29#endregion
 30
 31
 32namespace CSWinFormDataGridView.CustomDataGridViewColumn
 33{
 34    public partial class MainForm : Form
 35    {
 36        public MainForm()
 37        {
 38            InitializeComponent();
 39        }
 40
 41        private void MainForm_Load(object sender, EventArgs e)
 42        {
 43            DataGridViewTextBoxColumn dgvTextBoxColumn;
 44            MaskedTextBoxColumn mtbColumn;
 45
 46            //
 47            // Employee name.
 48            //
 49            dgvTextBoxColumn = new DataGridViewTextBoxColumn();
 50            dgvTextBoxColumn.HeaderText = "Name";
 51            dgvTextBoxColumn.Width = 120;
 52            this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
 53
 54            //
 55            // Employee ID -- this will be of the format:
 56            // [A-Z][0-9][0-9][0-9][0-9][0-9]
 57            //
 58            // this is well sutied to using a MaskedTextBox column.
 59            //
 60            mtbColumn = new MaskedTextBoxColumn();
 61            mtbColumn.HeaderText = "Employee ID";
 62            mtbColumn.Mask = "L00000";
 63            mtbColumn.Width = 75;
 64            this.employeesDataGridView.Columns.Add(mtbColumn);
 65
 66            //
 67            // [American] Social Security number, of the format:
 68            // ###-##-####
 69            // 
 70            mtbColumn = new MaskedTextBoxColumn();
 71            mtbColumn.HeaderText = "SSN";
 72            mtbColumn.Mask = "000-00-0000";
 73            mtbColumn.Width = 75;
 74            this.employeesDataGridView.Columns.Add(mtbColumn);
 75
 76            //
 77            // Address
 78            //
 79            dgvTextBoxColumn = new DataGridViewTextBoxColumn();
 80            dgvTextBoxColumn.HeaderText = "Address";
 81            dgvTextBoxColumn.Width = 150;
 82            this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
 83
 84            //
 85            // City
 86            //
 87            dgvTextBoxColumn = new DataGridViewTextBoxColumn();
 88            dgvTextBoxColumn.HeaderText = "City";
 89            dgvTextBoxColumn.Width = 75;
 90            this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
 91
 92            //
 93            // State
 94            //
 95            mtbColumn = new MaskedTextBoxColumn();
 96            mtbColumn.HeaderText = "State";
 97            mtbColumn.Mask = "LL";
 98            mtbColumn.Width = 40;
 99            this.employeesDataGridView.Columns.Add(mtbColumn);
100
101            //
102            // Zip Code #####-#### (+4 optional)
103            //
104            mtbColumn = new MaskedTextBoxColumn();
105            mtbColumn.HeaderText = "Zip Code";
106            mtbColumn.Mask = "00000-0000";
107            mtbColumn.Width = 75;
108            mtbColumn.ValidatingType = typeof(ZipCode);
109            this.employeesDataGridView.Columns.Add(mtbColumn);
110
111
112            //
113            // Department Code
114            //
115            dgvTextBoxColumn = new DataGridViewTextBoxColumn();
116            dgvTextBoxColumn.HeaderText = "Department";
117            dgvTextBoxColumn.ValueType = typeof(int);
118            dgvTextBoxColumn.Width = 75;
119            this.employeesDataGridView.Columns.Add(dgvTextBoxColumn);
120        }
121    }
122
123    // Type that represents a US Zipcode to demonstrate
124    // the ValidatingType feature on the MaskedTextBox.
125    #region ZipCode Class
126
127    public class ZipCode
128    {
129        private int zipCode;
130        private int plusFour;
131
132        public ZipCode()
133        {
134            this.zipCode = 0;
135            this.plusFour = 0;
136        }
137
138        public ZipCode(string in_zipCode)
139        {
140            parseFromString(in_zipCode, out zipCode, out plusFour);
141        }
142
143        public ZipCode(int in_ivalue)
144        {
145            this.zipCode = in_ivalue;
146            this.plusFour = 0;
147        }
148
149
150        public static implicit operator ZipCode(string s)
151        {
152            return new ZipCode(s);
153        }
154
155        public static implicit operator ZipCode(int i)
156        {
157            return new ZipCode(i);
158        }
159
160
161        protected static void parseFromString
162        (
163            string in_string,
164            out int out_zipCode,
165            out int out_plusFour
166        )
167        {
168            int zc = 0, pf = 0;
169            char[] charray;
170            int x = 0;
171
172            if (in_string == null || in_string.Equals(""))
173            {
174                throw new ArgumentException("Invalid String");
175            }
176
177            charray = in_string.Trim().ToCharArray();
178
179            for (x = 0; x < 5; x++)
180            {
181                if (!Char.IsDigit(charray[x]))
182                {
183                    throw new ArgumentException("Invalid String");
184                }
185
186                zc = zc * 10 + numfromchar(charray[x]);
187            }
188
189            while (x < charray.Length && !Char.IsDigit(charray[x]))
190            {
191                x++;
192            }
193
194            if (x < charray.Length)
195            {
196                for (int y = x; y < 4; y++)
197                {
198                    if (!Char.IsDigit(charray[y]))
199                    {
200                        throw new ArgumentException("Invalid ZipCode String");
201                    }
202
203                    pf = pf * 10 + numfromchar(charray[y]);
204                }
205            }
206
207            out_zipCode = zc;
208            out_plusFour = pf;
209        }
210
211
212        private static int numfromchar(char c)
213        {
214            switch (c)
215            {
216                case '0':
217                    return 0;
218                case '1':
219                    return 1;
220                case '2':
221                    return 2;
222                case '3':
223                    return 3;
224                case '4':
225                    return 4;
226                case '5':
227                    return 5;
228                case '6':
229                    return 6;
230                case '7':
231                    return 7;
232                case '8':
233                    return 8;
234                case '9':
235                    return 9;
236
237                default:
238                    throw new ArgumentException("invalid digit");
239            }
240        }
241
242        public override string ToString()
243        {
244            StringBuilder sb = new StringBuilder(10);
245
246            sb.Append(zipCode.ToString("00000"));
247            sb.Append("-");
248            sb.Append(plusFour.ToString("0000"));
249
250            return sb.ToString();
251        }
252    }
253    
254    #endregion 
255
256}