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