PageRenderTime 44ms CodeModel.GetById 28ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 342 lines | 204 code | 32 blank | 106 comment | 43 complexity | a42581e768be034d00798eb10a8aea98 MD5 | raw file
  1/********************************* Module Header **********************************\
  2* Module Name:	MaskedTextBoxColumn.cs
  3* Project:		CSWinFormDataGridView
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* This sample demonstrates how to create a custom DataGridView column.
  7* 
  8* This source is subject to the Microsoft Public License.
  9* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 10* All other rights reserved.
 11* 
 12* History:
 13* * 6/05/2009 3:00 PM Zhi-Xin Ye Created
 14\**********************************************************************************/
 15
 16#region Using directives
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20using System.Text;
 21using System.Windows.Forms;
 22#endregion
 23
 24
 25namespace CSWinFormDataGridView.CustomDataGridViewColumn
 26{
 27    /// <summary>
 28    /// The base object for the custom column type.  Programmers manipulate
 29    /// the column types most often when working with the DataGridView, and
 30    /// this one sets the basics and Cell Template values controlling the
 31    /// default behaviour for cells of this column type.
 32    /// </summary>
 33    public class MaskedTextBoxColumn : DataGridViewColumn
 34    {
 35        private string mask;
 36        private char promptChar;
 37        private bool includePrompt;
 38        private bool includeLiterals;
 39        private Type validatingType;
 40
 41        /// <summary>
 42        /// Initializes a new instance of this class, making sure to pass
 43        /// to its base constructor an instance of a MaskedTextBoxCell 
 44        /// class to use as the basic template.
 45        /// </summary>
 46        public MaskedTextBoxColumn()
 47            : base(new MaskedTextBoxCell())
 48        {
 49        }
 50
 51        /// <summary>
 52        /// Routine to convert from boolean to DataGridViewTriState.
 53        /// </summary>
 54        /// <param name="value"></param>
 55        /// <returns></returns>
 56        private static DataGridViewTriState TriBool(bool value)
 57        {
 58            return value ? DataGridViewTriState.True
 59                         : DataGridViewTriState.False;
 60        }
 61
 62
 63        /// <summary>
 64        /// The template cell that will be used for this column by default,
 65        /// unless a specific cell is set for a particular row.
 66        /// 
 67        /// A MaskedTextBoxCell cell which will serve as the template cell
 68        /// for this column.
 69        /// </summary>
 70        public override DataGridViewCell CellTemplate
 71        {
 72            get
 73            {
 74                return base.CellTemplate;
 75            }
 76
 77            set
 78            {
 79                //  Only cell types that derive from MaskedTextBoxCell are supported 
 80                // as the cell template.
 81                if (value != null && !value.GetType().IsAssignableFrom(
 82                    typeof(MaskedTextBoxCell)))
 83                {
 84                    string s = "Cell type is not based upon the MaskedTextBoxCell.";
 85                    //CustomColumnMain.GetResourceManager().GetString("excNotMaskedTextBox");
 86                    throw new InvalidCastException(s);
 87                }
 88
 89                base.CellTemplate = value;
 90            }
 91        }
 92
 93        /// <summary>
 94        /// Indicates the Mask property that is used on the MaskedTextBox
 95        /// for entering new data into cells of this type.
 96        /// 
 97        /// See the MaskedTextBox control documentation for more details.
 98        /// </summary>
 99        public virtual string Mask
100        {
101            get
102            {
103                return this.mask;
104            }
105            set
106            {
107                MaskedTextBoxCell mtbCell;
108                DataGridViewCell dgvCell;
109                int rowCount;
110
111                if (this.mask != value)
112                {
113                    this.mask = value;
114
115                    //
116                    // First, update the value on the template cell.
117                    //
118                    mtbCell = (MaskedTextBoxCell)this.CellTemplate;
119                    mtbCell.Mask = value;
120
121                    //
122                    // Now set it on all cells in other rows as well.
123                    //
124                    if (this.DataGridView != null && this.DataGridView.Rows != null)
125                    {
126                        rowCount = this.DataGridView.Rows.Count;
127                        for (int x = 0; x < rowCount; x++)
128                        {
129                            dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
130                            if (dgvCell is MaskedTextBoxCell)
131                            {
132                                mtbCell = (MaskedTextBoxCell)dgvCell;
133                                mtbCell.Mask = value;
134                            }
135                        }
136                    }
137                }
138            }
139        }
140
141
142        /// <summary>
143        /// By default, the MaskedTextBox uses the underscore (_) character
144        /// to prompt for required characters.  This propertly lets you 
145        /// choose a different one.
146        /// 
147        /// See the MaskedTextBox control documentation for more details.
148        /// </summary>
149        public virtual char PromptChar
150        {
151            get
152            {
153                return this.promptChar;
154            }
155            set
156            {
157                MaskedTextBoxCell mtbCell;
158                DataGridViewCell dgvCell;
159                int rowCount;
160
161                if (this.promptChar != value)
162                {
163                    this.promptChar = value;
164
165                    //
166                    // First, update the value on the template cell.
167                    //
168                    mtbCell = (MaskedTextBoxCell)this.CellTemplate;
169                    mtbCell.PromptChar = value;
170
171                    //
172                    // Now set it on all cells in other rows as well.
173                    //
174                    if (this.DataGridView != null && this.DataGridView.Rows != null)
175                    {
176                        rowCount = this.DataGridView.Rows.Count;
177                        for (int x = 0; x < rowCount; x++)
178                        {
179                            dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
180                            if (dgvCell is MaskedTextBoxCell)
181                            {
182                                mtbCell = (MaskedTextBoxCell)dgvCell;
183                                mtbCell.PromptChar = value;
184                            }
185                        }
186                    }
187                }
188            }
189        }
190
191        /// <summary>
192        /// Indicates whether any unfilled characters in the mask should be
193        /// be included as prompt characters when somebody asks for the text
194        /// of the MaskedTextBox for a particular cell programmatically.
195        /// 
196        /// See the MaskedTextBox control documentation for more details.
197        /// </summary>
198        public virtual bool IncludePrompt
199        {
200            get
201            {
202                return this.includePrompt;
203            }
204            set
205            {
206                MaskedTextBoxCell mtbc;
207                DataGridViewCell dgvc;
208                int rowCount;
209
210                if (this.includePrompt != value)
211                {
212                    this.includePrompt = value;
213
214                    //
215                    // First, update the value on the template cell.
216                    //
217                    mtbc = (MaskedTextBoxCell)this.CellTemplate;
218                    mtbc.IncludePrompt = TriBool(value);
219
220                    //
221                    // Now set it on all cells in other rows as well.
222                    //
223                    if (this.DataGridView != null && this.DataGridView.Rows != null)
224                    {
225                        rowCount = this.DataGridView.Rows.Count;
226                        for (int x = 0; x < rowCount; x++)
227                        {
228                            dgvc = this.DataGridView.Rows.SharedRow(x).Cells[x];
229                            if (dgvc is MaskedTextBoxCell)
230                            {
231                                mtbc = (MaskedTextBoxCell)dgvc;
232                                mtbc.IncludePrompt = TriBool(value);
233                            }
234                        }
235                    }
236                }
237            }
238        }
239
240        /// <summary>
241        /// Controls whether or not literal (non-prompt) characters should
242        /// be included in the output of the Text property for newly entered
243        /// data in a cell of this type.
244        /// 
245        /// See the MaskedTextBox control documentation for more details.
246        /// </summary>
247        public virtual bool IncludeLiterals
248        {
249            get
250            {
251                return this.includeLiterals;
252            }
253            set
254            {
255                MaskedTextBoxCell mtbCell;
256                DataGridViewCell dgvCell;
257                int rowCount;
258
259                if (this.includeLiterals != value)
260                {
261                    this.includeLiterals = value;
262
263                    //
264                    // First, update the value on the template cell.
265                    //
266                    mtbCell = (MaskedTextBoxCell)this.CellTemplate;
267                    mtbCell.IncludeLiterals = TriBool(value);
268
269                    //
270                    // Now set it on all cells in other rows as well.
271                    //
272                    if (this.DataGridView != null && this.DataGridView.Rows != null)
273                    {
274
275                        rowCount = this.DataGridView.Rows.Count;
276                        for (int x = 0; x < rowCount; x++)
277                        {
278                            dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
279                            if (dgvCell is MaskedTextBoxCell)
280                            {
281                                mtbCell = (MaskedTextBoxCell)dgvCell;
282                                mtbCell.IncludeLiterals = TriBool(value);
283                            }
284                        }
285                    }
286                }
287            }
288        }
289
290        /// <summary>
291        /// Indicates the type against any data entered in the MaskedTextBox
292        /// should be validated.  The MaskedTextBox control will attempt to
293        /// instantiate this type and assign the value from the contents of
294        /// the text box.  An error will occur if it fails to assign to this
295        /// type.
296        ///
297        /// See the MaskedTextBox control documentation for more details.
298        /// </summary>
299        public virtual Type ValidatingType
300        {
301            get
302            {
303                return this.validatingType;
304            }
305            set
306            {
307                MaskedTextBoxCell mtbCell;
308                DataGridViewCell dgvCell;
309                int rowCount;
310
311                if (this.validatingType != value)
312                {
313                    this.validatingType = value;
314
315                    //
316                    // First, update the value on the template cell.
317                    //
318                    mtbCell = (MaskedTextBoxCell)this.CellTemplate;
319                    mtbCell.ValidatingType = value;
320
321                    //
322                    // Now set it on all cells in other rows as well.
323                    //
324                    if (this.DataGridView != null && this.DataGridView.Rows != null)
325                    {
326                        rowCount = this.DataGridView.Rows.Count;
327                        for (int x = 0; x < rowCount; x++)
328                        {
329                            dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
330                            if (dgvCell is MaskedTextBoxCell)
331                            {
332                                mtbCell = (MaskedTextBoxCell)dgvCell;
333                                mtbCell.ValidatingType = value;
334                            }
335                        }
336                    }
337                }
338            }
339        }
340
341    }
342}