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