PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 342 lines | 204 code | 32 blank | 106 comment | 43 complexity | a42581e768be034d00798eb10a8aea98 MD5 | raw file
  1. /********************************* Module Header **********************************\
  2. * Module Name: MaskedTextBoxColumn.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. /// <summary>
  25. /// The base object for the custom column type. Programmers manipulate
  26. /// the column types most often when working with the DataGridView, and
  27. /// this one sets the basics and Cell Template values controlling the
  28. /// default behaviour for cells of this column type.
  29. /// </summary>
  30. public class MaskedTextBoxColumn : DataGridViewColumn
  31. {
  32. private string mask;
  33. private char promptChar;
  34. private bool includePrompt;
  35. private bool includeLiterals;
  36. private Type validatingType;
  37. /// <summary>
  38. /// Initializes a new instance of this class, making sure to pass
  39. /// to its base constructor an instance of a MaskedTextBoxCell
  40. /// class to use as the basic template.
  41. /// </summary>
  42. public MaskedTextBoxColumn()
  43. : base(new MaskedTextBoxCell())
  44. {
  45. }
  46. /// <summary>
  47. /// Routine to convert from boolean to DataGridViewTriState.
  48. /// </summary>
  49. /// <param name="value"></param>
  50. /// <returns></returns>
  51. private static DataGridViewTriState TriBool(bool value)
  52. {
  53. return value ? DataGridViewTriState.True
  54. : DataGridViewTriState.False;
  55. }
  56. /// <summary>
  57. /// The template cell that will be used for this column by default,
  58. /// unless a specific cell is set for a particular row.
  59. ///
  60. /// A MaskedTextBoxCell cell which will serve as the template cell
  61. /// for this column.
  62. /// </summary>
  63. public override DataGridViewCell CellTemplate
  64. {
  65. get
  66. {
  67. return base.CellTemplate;
  68. }
  69. set
  70. {
  71. // Only cell types that derive from MaskedTextBoxCell are supported
  72. // as the cell template.
  73. if (value != null && !value.GetType().IsAssignableFrom(
  74. typeof(MaskedTextBoxCell)))
  75. {
  76. string s = "Cell type is not based upon the MaskedTextBoxCell.";
  77. //CustomColumnMain.GetResourceManager().GetString("excNotMaskedTextBox");
  78. throw new InvalidCastException(s);
  79. }
  80. base.CellTemplate = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Indicates the Mask property that is used on the MaskedTextBox
  85. /// for entering new data into cells of this type.
  86. ///
  87. /// See the MaskedTextBox control documentation for more details.
  88. /// </summary>
  89. public virtual string Mask
  90. {
  91. get
  92. {
  93. return this.mask;
  94. }
  95. set
  96. {
  97. MaskedTextBoxCell mtbCell;
  98. DataGridViewCell dgvCell;
  99. int rowCount;
  100. if (this.mask != value)
  101. {
  102. this.mask = value;
  103. //
  104. // First, update the value on the template cell.
  105. //
  106. mtbCell = (MaskedTextBoxCell)this.CellTemplate;
  107. mtbCell.Mask = value;
  108. //
  109. // Now set it on all cells in other rows as well.
  110. //
  111. if (this.DataGridView != null && this.DataGridView.Rows != null)
  112. {
  113. rowCount = this.DataGridView.Rows.Count;
  114. for (int x = 0; x < rowCount; x++)
  115. {
  116. dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
  117. if (dgvCell is MaskedTextBoxCell)
  118. {
  119. mtbCell = (MaskedTextBoxCell)dgvCell;
  120. mtbCell.Mask = value;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// By default, the MaskedTextBox uses the underscore (_) character
  129. /// to prompt for required characters. This propertly lets you
  130. /// choose a different one.
  131. ///
  132. /// See the MaskedTextBox control documentation for more details.
  133. /// </summary>
  134. public virtual char PromptChar
  135. {
  136. get
  137. {
  138. return this.promptChar;
  139. }
  140. set
  141. {
  142. MaskedTextBoxCell mtbCell;
  143. DataGridViewCell dgvCell;
  144. int rowCount;
  145. if (this.promptChar != value)
  146. {
  147. this.promptChar = value;
  148. //
  149. // First, update the value on the template cell.
  150. //
  151. mtbCell = (MaskedTextBoxCell)this.CellTemplate;
  152. mtbCell.PromptChar = value;
  153. //
  154. // Now set it on all cells in other rows as well.
  155. //
  156. if (this.DataGridView != null && this.DataGridView.Rows != null)
  157. {
  158. rowCount = this.DataGridView.Rows.Count;
  159. for (int x = 0; x < rowCount; x++)
  160. {
  161. dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
  162. if (dgvCell is MaskedTextBoxCell)
  163. {
  164. mtbCell = (MaskedTextBoxCell)dgvCell;
  165. mtbCell.PromptChar = value;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// Indicates whether any unfilled characters in the mask should be
  174. /// be included as prompt characters when somebody asks for the text
  175. /// of the MaskedTextBox for a particular cell programmatically.
  176. ///
  177. /// See the MaskedTextBox control documentation for more details.
  178. /// </summary>
  179. public virtual bool IncludePrompt
  180. {
  181. get
  182. {
  183. return this.includePrompt;
  184. }
  185. set
  186. {
  187. MaskedTextBoxCell mtbc;
  188. DataGridViewCell dgvc;
  189. int rowCount;
  190. if (this.includePrompt != value)
  191. {
  192. this.includePrompt = value;
  193. //
  194. // First, update the value on the template cell.
  195. //
  196. mtbc = (MaskedTextBoxCell)this.CellTemplate;
  197. mtbc.IncludePrompt = TriBool(value);
  198. //
  199. // Now set it on all cells in other rows as well.
  200. //
  201. if (this.DataGridView != null && this.DataGridView.Rows != null)
  202. {
  203. rowCount = this.DataGridView.Rows.Count;
  204. for (int x = 0; x < rowCount; x++)
  205. {
  206. dgvc = this.DataGridView.Rows.SharedRow(x).Cells[x];
  207. if (dgvc is MaskedTextBoxCell)
  208. {
  209. mtbc = (MaskedTextBoxCell)dgvc;
  210. mtbc.IncludePrompt = TriBool(value);
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// Controls whether or not literal (non-prompt) characters should
  219. /// be included in the output of the Text property for newly entered
  220. /// data in a cell of this type.
  221. ///
  222. /// See the MaskedTextBox control documentation for more details.
  223. /// </summary>
  224. public virtual bool IncludeLiterals
  225. {
  226. get
  227. {
  228. return this.includeLiterals;
  229. }
  230. set
  231. {
  232. MaskedTextBoxCell mtbCell;
  233. DataGridViewCell dgvCell;
  234. int rowCount;
  235. if (this.includeLiterals != value)
  236. {
  237. this.includeLiterals = value;
  238. //
  239. // First, update the value on the template cell.
  240. //
  241. mtbCell = (MaskedTextBoxCell)this.CellTemplate;
  242. mtbCell.IncludeLiterals = TriBool(value);
  243. //
  244. // Now set it on all cells in other rows as well.
  245. //
  246. if (this.DataGridView != null && this.DataGridView.Rows != null)
  247. {
  248. rowCount = this.DataGridView.Rows.Count;
  249. for (int x = 0; x < rowCount; x++)
  250. {
  251. dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
  252. if (dgvCell is MaskedTextBoxCell)
  253. {
  254. mtbCell = (MaskedTextBoxCell)dgvCell;
  255. mtbCell.IncludeLiterals = TriBool(value);
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// Indicates the type against any data entered in the MaskedTextBox
  264. /// should be validated. The MaskedTextBox control will attempt to
  265. /// instantiate this type and assign the value from the contents of
  266. /// the text box. An error will occur if it fails to assign to this
  267. /// type.
  268. ///
  269. /// See the MaskedTextBox control documentation for more details.
  270. /// </summary>
  271. public virtual Type ValidatingType
  272. {
  273. get
  274. {
  275. return this.validatingType;
  276. }
  277. set
  278. {
  279. MaskedTextBoxCell mtbCell;
  280. DataGridViewCell dgvCell;
  281. int rowCount;
  282. if (this.validatingType != value)
  283. {
  284. this.validatingType = value;
  285. //
  286. // First, update the value on the template cell.
  287. //
  288. mtbCell = (MaskedTextBoxCell)this.CellTemplate;
  289. mtbCell.ValidatingType = value;
  290. //
  291. // Now set it on all cells in other rows as well.
  292. //
  293. if (this.DataGridView != null && this.DataGridView.Rows != null)
  294. {
  295. rowCount = this.DataGridView.Rows.Count;
  296. for (int x = 0; x < rowCount; x++)
  297. {
  298. dgvCell = this.DataGridView.Rows.SharedRow(x).Cells[x];
  299. if (dgvCell is MaskedTextBoxCell)
  300. {
  301. mtbCell = (MaskedTextBoxCell)dgvCell;
  302. mtbCell.ValidatingType = value;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }