PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 278 lines | 175 code | 44 blank | 59 comment | 18 complexity | 6bdcb7980465c4b4ded7f66d7f91bf16 MD5 | raw file
  1/********************************* Module Header **********************************\
  2* Module Name:	MaskedTextBoxEditingControl.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    public class MaskedTextBoxEditingControl : MaskedTextBox, IDataGridViewEditingControl
 28    {
 29        protected int rowIndex;
 30        protected DataGridView dataGridView;
 31        protected bool valueChanged = false;
 32
 33        public MaskedTextBoxEditingControl()
 34        {
 35            
 36        }
 37
 38        protected override void OnTextChanged(EventArgs e)
 39        {
 40            base.OnTextChanged(e);
 41
 42            // Let the DataGridView know about the value change
 43            NotifyDataGridViewOfValueChange();
 44        }
 45
 46        /// <summary>
 47        /// Notify DataGridView that the value has changed.
 48        /// </summary>
 49        protected virtual void NotifyDataGridViewOfValueChange()
 50        {
 51            this.valueChanged = true;
 52            if (this.dataGridView != null)
 53            {
 54                this.dataGridView.NotifyCurrentCellDirty(true);
 55            }
 56        }
 57
 58
 59        #region IDataGridViewEditingControl Members
 60
 61        //  Indicates the cursor that should be shown when the user hovers their
 62        //  mouse over this cell when the editing control is shown.
 63        public Cursor EditingPanelCursor
 64        {
 65            get
 66            {
 67                return Cursors.IBeam;
 68            }
 69        }
 70
 71
 72        //  Returns or sets the parent DataGridView.
 73        public DataGridView EditingControlDataGridView
 74        {
 75            get
 76            {
 77                return this.dataGridView;
 78            }
 79
 80            set
 81            {
 82                this.dataGridView = value;
 83            }
 84        }
 85
 86
 87        //  Sets/Gets the formatted value contents of this cell.
 88        public object EditingControlFormattedValue
 89        {
 90            set
 91            {
 92                this.Text = value.ToString();
 93                NotifyDataGridViewOfValueChange();
 94            }
 95            get
 96            {
 97                return this.Text;
 98            }
 99
100        }
101
102        //   Get the value of the editing control for formatting.
103        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
104        {
105            return this.Text;
106        }
107
108        //  Process input key and determine if the key should be used for the editing control
109        //  or allowed to be processed by the grid. Handle cursor movement keys for the MaskedTextBox
110        //  control; otherwise if the DataGridView doesn't want the input key then let the editing control handle it.
111        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
112        {
113            switch (keyData & Keys.KeyCode)
114            {
115                case Keys.Right:
116                    //
117                    // If the end of the selection is at the end of the string
118                    // let the DataGridView treat the key message
119                    //
120                    if (!(this.SelectionLength == 0
121                          && this.SelectionStart == this.ToString().Length))
122                    {
123                        return true;
124                    }
125                    break;
126
127                case Keys.Left:
128                    //
129                    // If the end of the selection is at the begining of the
130                    // string or if the entire text is selected send this character 
131                    // to the dataGridView; else process the key event.
132                    //
133                    if (!(this.SelectionLength == 0
134                          && this.SelectionStart == 0))
135                    {
136                        return true;
137                    }
138                    break;
139
140                case Keys.Home:
141                case Keys.End:
142                    if (this.SelectionLength != this.ToString().Length)
143                    {
144                        return true;
145                    }
146                    break;
147
148                case Keys.Prior:
149                case Keys.Next:
150                    if (this.valueChanged)
151                    {
152                        return true;
153                    }
154                    break;
155
156                case Keys.Delete:
157                    if (this.SelectionLength > 0 || this.SelectionStart < this.ToString().Length)
158                    {
159                        return true;
160                    }
161                    break;
162            }
163
164            //
165            // defer to the DataGridView and see if it wants it.
166            //
167            return !dataGridViewWantsInputKey;
168        }
169
170
171        //  Prepare the editing control for edit.
172        public void PrepareEditingControlForEdit(bool selectAll)
173        {
174            if (selectAll)
175            {
176                SelectAll();
177            }
178            else
179            {
180                //
181                // Do not select all the text, but position the caret at the 
182                // end of the text.
183                //
184                this.SelectionStart = this.ToString().Length;
185            }
186        }
187
188        //  Indicates whether or not the parent DataGridView control should
189        //  reposition the editing control every time value change is indicated.
190        //  There is no need to do this for the MaskedTextBox.
191        public bool RepositionEditingControlOnValueChange
192        {
193            get
194            {
195                return false;
196            }
197        }
198
199
200        //  Indicates the row index of this cell.  This is often -1 for the
201        //  template cell, but for other cells, might actually have a value
202        //  greater than or equal to zero.
203        public int EditingControlRowIndex
204        {
205            get
206            {
207                return this.rowIndex;
208            }
209
210            set
211            {
212                this.rowIndex = value;
213            }
214        }
215
216
217
218        //  Make the MaskedTextBox control match the style and colors of
219        //  the host DataGridView control and other editing controls 
220        //  before showing the editing control.
221        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
222        {
223            this.Font = dataGridViewCellStyle.Font;
224            this.ForeColor = dataGridViewCellStyle.ForeColor;
225            this.BackColor = dataGridViewCellStyle.BackColor;
226            this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
227        }
228
229
230        //  Gets or sets our flag indicating whether the value has changed.
231        public bool EditingControlValueChanged
232        {
233            get
234            {
235                return valueChanged;
236            }
237
238            set
239            {
240                this.valueChanged = value;
241            }
242        }
243
244        #endregion // IDataGridViewEditingControl.
245
246
247        /// <summary>
248        /// Routine to translate between DataGridView content alignments and text 
249        /// box horizontal alignments.
250        /// </summary>
251        /// <param name="align"></param>
252        /// <returns></returns>
253        private static HorizontalAlignment translateAlignment(DataGridViewContentAlignment align)
254        {
255            switch (align)
256            {
257                case DataGridViewContentAlignment.TopLeft:
258                case DataGridViewContentAlignment.MiddleLeft:
259                case DataGridViewContentAlignment.BottomLeft:
260                    return HorizontalAlignment.Left;
261
262                case DataGridViewContentAlignment.TopCenter:
263                case DataGridViewContentAlignment.MiddleCenter:
264                case DataGridViewContentAlignment.BottomCenter:
265                    return HorizontalAlignment.Center;
266
267                case DataGridViewContentAlignment.TopRight:
268                case DataGridViewContentAlignment.MiddleRight:
269                case DataGridViewContentAlignment.BottomRight:
270                    return HorizontalAlignment.Right;
271            }
272
273            throw new ArgumentException("Error: Invalid Content Alignment!");
274        }
275
276
277    }
278}