PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 242 lines | 138 code | 23 blank | 81 comment | 9 complexity | 684a1347acbc99a5fb3169dbe541426d MD5 | raw file
  1/********************************* Module Header **********************************\
  2* Module Name:	MaskedTextBoxCell.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    class MaskedTextBoxCell : DataGridViewTextBoxCell
 28    {
 29        private string mask;
 30        private char promptChar;
 31        private DataGridViewTriState includePrompt;
 32        private DataGridViewTriState includeLiterals;
 33        private Type validatingType;
 34
 35        /// <summary>
 36        /// Initializes a new instance of this class.  Fortunately, there's 
 37        /// not much to do here except make sure that our base class is also 
 38        /// initialized properly.
 39        /// </summary>
 40        public MaskedTextBoxCell()
 41            : base()
 42        {
 43            this.mask = "";
 44            this.promptChar = '_';
 45            this.includePrompt = DataGridViewTriState.NotSet;
 46            this.includeLiterals = DataGridViewTriState.NotSet;
 47            this.validatingType = typeof(string);
 48        }
 49
 50        /// <summary>
 51        /// Whenever the user is to begin editing a cell of this type, the editing
 52        /// control must be created, which in this column type's case is a subclass
 53        /// of the MaskedTextBox control.
 54        /// 
 55        /// This routine sets up all the properties and values on this control 
 56        /// before the editing begins.
 57        /// </summary>
 58        /// <param name="rowIndex"></param>
 59        /// <param name="initialFormattedValue"></param>
 60        /// <param name="dataGridViewCellStyle"></param>
 61        public override void InitializeEditingControl(int rowIndex,
 62            object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
 63        {
 64            MaskedTextBoxEditingControl mtbEditingCtrl;
 65            MaskedTextBoxColumn mtbColumn;
 66            DataGridViewColumn dgvColumn;
 67
 68            base.InitializeEditingControl(rowIndex, initialFormattedValue,
 69                                          dataGridViewCellStyle);
 70
 71            mtbEditingCtrl = DataGridView.EditingControl as MaskedTextBoxEditingControl;
 72
 73            //
 74            // Set up props that are specific to the MaskedTextBox
 75            //
 76
 77            dgvColumn = this.OwningColumn;   // this.DataGridView.Columns[this.ColumnIndex];
 78            if (dgvColumn is MaskedTextBoxColumn)
 79            {
 80                mtbColumn = dgvColumn as MaskedTextBoxColumn;
 81
 82                //
 83                // get the mask from this instance or the parent column.
 84                //
 85                if (string.IsNullOrEmpty(this.mask))
 86                {
 87                    mtbEditingCtrl.Mask = mtbColumn.Mask;
 88                }
 89                else
 90                {
 91                    mtbEditingCtrl.Mask = this.mask;
 92                }
 93
 94                //
 95                // Prompt char.
 96                //
 97                mtbEditingCtrl.PromptChar = this.PromptChar;
 98
 99                //
100                // IncludePrompt
101                //
102                if (this.includePrompt == DataGridViewTriState.NotSet)
103                {
104                    //mtbEditingCtrl.IncludePrompt = mtbcol.IncludePrompt;
105                }
106                else
107                {
108                    //mtbEditingCtrl.IncludePrompt = BoolFromTri(this.includePrompt);
109                }
110
111                //
112                // IncludeLiterals
113                //
114                if (this.includeLiterals == DataGridViewTriState.NotSet)
115                {
116                    //mtbEditingCtrl.IncludeLiterals = mtbcol.IncludeLiterals;
117                }
118                else
119                {
120                    //mtbEditingCtrl.IncludeLiterals = BoolFromTri(this.includeLiterals);
121                }
122
123                //
124                // Finally, the validating type ...
125                //
126                if (this.ValidatingType == null)
127                {
128                    mtbEditingCtrl.ValidatingType = mtbColumn.ValidatingType;
129                }
130                else
131                {
132                    mtbEditingCtrl.ValidatingType = this.ValidatingType;
133                }
134
135                mtbEditingCtrl.Text = (string)this.Value;
136            }
137        }
138
139        /// <summary>
140        /// Returns the type of the control that will be used for editing
141        /// cells of this type.  This control must be a valid Windows Forms
142        /// control and must implement IDataGridViewEditingControl.
143        /// </summary>
144        public override Type EditType
145        {
146            get
147            {
148                return typeof(MaskedTextBoxEditingControl);
149            }
150        }
151
152        /// <summary>
153        /// A string value containing the Mask against input for cells of
154        /// this type will be verified.
155        /// </summary>
156        public virtual string Mask
157        {
158            get
159            {
160                return this.mask;
161            }
162            set
163            {
164                this.mask = value;
165            }
166        }
167
168        /// <summary>
169        /// The character to use for prompting for new input.
170        /// </summary>
171        public virtual char PromptChar
172        {
173            get
174            {
175                return this.promptChar;
176            }
177            set
178            {
179                this.promptChar = value;
180            }
181        }
182
183
184        /// <summary>
185        /// A boolean indicating whether to include prompt characters in
186        /// the Text property's value.
187        /// </summary>
188        public virtual DataGridViewTriState IncludePrompt
189        {
190            get
191            {
192                return this.includePrompt;
193            }
194            set
195            {
196                this.includePrompt = value;
197            }
198        }
199
200        /// <summary>
201        /// A boolean value indicating whether to include literal characters
202        /// in the Text property's output value.
203        /// </summary>
204        public virtual DataGridViewTriState IncludeLiterals
205        {
206            get
207            {
208                return this.includeLiterals;
209            }
210            set
211            {
212                this.includeLiterals = value;
213            }
214        }
215
216        /// <summary>
217        /// A Type object for the validating type.
218        /// </summary>
219        public virtual Type ValidatingType
220        {
221            get
222            {
223                return this.validatingType;
224            }
225            set
226            {
227                this.validatingType = value;
228            }
229        }
230
231        /// <summary>
232        /// Quick routine to convert from DataGridViewTriState to boolean.
233        /// True goes to true while False and NotSet go to false.
234        /// </summary>
235        /// <param name="tri"></param>
236        /// <returns></returns>
237        protected static bool BoolFromTri(DataGridViewTriState tri)
238        {
239            return (tri == DataGridViewTriState.True) ? true : false;
240        }
241    }
242}