/Visual Studio 2008/CSWinFormDataGridView/CustomDataGridViewColumn/MaskedTextBoxEditingControl.cs
C# | 278 lines | 175 code | 44 blank | 59 comment | 18 complexity | 6bdcb7980465c4b4ded7f66d7f91bf16 MD5 | raw file
- /********************************* Module Header **********************************\
- * Module Name: MaskedTextBoxEditingControl.cs
- * Project: CSWinFormDataGridView
- * Copyright (c) Microsoft Corporation.
- *
- * This sample demonstrates how to create a custom DataGridView column.
- *
- * This source is subject to the Microsoft Public License.
- * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
- * All other rights reserved.
- *
- * History:
- * * 6/05/2009 3:00 PM Zhi-Xin Ye Created
- \**********************************************************************************/
-
- #region Using directives
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- #endregion
-
-
- namespace CSWinFormDataGridView.CustomDataGridViewColumn
- {
- public class MaskedTextBoxEditingControl : MaskedTextBox, IDataGridViewEditingControl
- {
- protected int rowIndex;
- protected DataGridView dataGridView;
- protected bool valueChanged = false;
-
- public MaskedTextBoxEditingControl()
- {
-
- }
-
- protected override void OnTextChanged(EventArgs e)
- {
- base.OnTextChanged(e);
-
- // Let the DataGridView know about the value change
- NotifyDataGridViewOfValueChange();
- }
-
- /// <summary>
- /// Notify DataGridView that the value has changed.
- /// </summary>
- protected virtual void NotifyDataGridViewOfValueChange()
- {
- this.valueChanged = true;
- if (this.dataGridView != null)
- {
- this.dataGridView.NotifyCurrentCellDirty(true);
- }
- }
-
-
- #region IDataGridViewEditingControl Members
-
- // Indicates the cursor that should be shown when the user hovers their
- // mouse over this cell when the editing control is shown.
- public Cursor EditingPanelCursor
- {
- get
- {
- return Cursors.IBeam;
- }
- }
-
-
- // Returns or sets the parent DataGridView.
- public DataGridView EditingControlDataGridView
- {
- get
- {
- return this.dataGridView;
- }
-
- set
- {
- this.dataGridView = value;
- }
- }
-
-
- // Sets/Gets the formatted value contents of this cell.
- public object EditingControlFormattedValue
- {
- set
- {
- this.Text = value.ToString();
- NotifyDataGridViewOfValueChange();
- }
- get
- {
- return this.Text;
- }
-
- }
-
- // Get the value of the editing control for formatting.
- public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
- {
- return this.Text;
- }
-
- // Process input key and determine if the key should be used for the editing control
- // or allowed to be processed by the grid. Handle cursor movement keys for the MaskedTextBox
- // control; otherwise if the DataGridView doesn't want the input key then let the editing control handle it.
- public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
- {
- switch (keyData & Keys.KeyCode)
- {
- case Keys.Right:
- //
- // If the end of the selection is at the end of the string
- // let the DataGridView treat the key message
- //
- if (!(this.SelectionLength == 0
- && this.SelectionStart == this.ToString().Length))
- {
- return true;
- }
- break;
-
- case Keys.Left:
- //
- // If the end of the selection is at the begining of the
- // string or if the entire text is selected send this character
- // to the dataGridView; else process the key event.
- //
- if (!(this.SelectionLength == 0
- && this.SelectionStart == 0))
- {
- return true;
- }
- break;
-
- case Keys.Home:
- case Keys.End:
- if (this.SelectionLength != this.ToString().Length)
- {
- return true;
- }
- break;
-
- case Keys.Prior:
- case Keys.Next:
- if (this.valueChanged)
- {
- return true;
- }
- break;
-
- case Keys.Delete:
- if (this.SelectionLength > 0 || this.SelectionStart < this.ToString().Length)
- {
- return true;
- }
- break;
- }
-
- //
- // defer to the DataGridView and see if it wants it.
- //
- return !dataGridViewWantsInputKey;
- }
-
-
- // Prepare the editing control for edit.
- public void PrepareEditingControlForEdit(bool selectAll)
- {
- if (selectAll)
- {
- SelectAll();
- }
- else
- {
- //
- // Do not select all the text, but position the caret at the
- // end of the text.
- //
- this.SelectionStart = this.ToString().Length;
- }
- }
-
- // Indicates whether or not the parent DataGridView control should
- // reposition the editing control every time value change is indicated.
- // There is no need to do this for the MaskedTextBox.
- public bool RepositionEditingControlOnValueChange
- {
- get
- {
- return false;
- }
- }
-
-
- // Indicates the row index of this cell. This is often -1 for the
- // template cell, but for other cells, might actually have a value
- // greater than or equal to zero.
- public int EditingControlRowIndex
- {
- get
- {
- return this.rowIndex;
- }
-
- set
- {
- this.rowIndex = value;
- }
- }
-
-
-
- // Make the MaskedTextBox control match the style and colors of
- // the host DataGridView control and other editing controls
- // before showing the editing control.
- public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
- {
- this.Font = dataGridViewCellStyle.Font;
- this.ForeColor = dataGridViewCellStyle.ForeColor;
- this.BackColor = dataGridViewCellStyle.BackColor;
- this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
- }
-
-
- // Gets or sets our flag indicating whether the value has changed.
- public bool EditingControlValueChanged
- {
- get
- {
- return valueChanged;
- }
-
- set
- {
- this.valueChanged = value;
- }
- }
-
- #endregion // IDataGridViewEditingControl.
-
-
- /// <summary>
- /// Routine to translate between DataGridView content alignments and text
- /// box horizontal alignments.
- /// </summary>
- /// <param name="align"></param>
- /// <returns></returns>
- private static HorizontalAlignment translateAlignment(DataGridViewContentAlignment align)
- {
- switch (align)
- {
- case DataGridViewContentAlignment.TopLeft:
- case DataGridViewContentAlignment.MiddleLeft:
- case DataGridViewContentAlignment.BottomLeft:
- return HorizontalAlignment.Left;
-
- case DataGridViewContentAlignment.TopCenter:
- case DataGridViewContentAlignment.MiddleCenter:
- case DataGridViewContentAlignment.BottomCenter:
- return HorizontalAlignment.Center;
-
- case DataGridViewContentAlignment.TopRight:
- case DataGridViewContentAlignment.MiddleRight:
- case DataGridViewContentAlignment.BottomRight:
- return HorizontalAlignment.Right;
- }
-
- throw new ArgumentException("Error: Invalid Content Alignment!");
- }
-
-
- }
- }