PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/VBWinFormDataGridView/CustomDataGridViewColumn/MaskedTextBoxColumn.vb

#
Visual Basic | 217 lines | 157 code | 16 blank | 44 comment | 0 complexity | f0192ad6dc0368de903e7760bd19ed71 MD5 | raw file
  1. '********************************* Module Header **********************************\
  2. ' Module Name: MaskedTextBoxColumn.vb
  3. ' Project: VBWinFormDataGridView
  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. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  12. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  13. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  14. '**********************************************************************************/
  15. Namespace VBWinFormDataGridView.CustomDataGridViewColumn
  16. Public Class MaskedTextBoxColumn
  17. Inherits DataGridViewTextBoxColumn
  18. Private _mask As String
  19. Private _promptChar As Char
  20. Private _includePrompt As Boolean
  21. Private _includeLiterals As Boolean
  22. Private _validatingType As Type
  23. Public Sub New()
  24. MyBase.CellTemplate = New MaskedTextBoxCell
  25. End Sub
  26. Private Shared Function TriBool(ByVal value As Boolean) As DataGridViewTriState
  27. If value Then
  28. Return DataGridViewTriState.True
  29. Else
  30. Return DataGridViewTriState.False
  31. End If
  32. End Function
  33. Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
  34. Get
  35. Return MyBase.CellTemplate
  36. End Get
  37. Set(ByVal value As System.Windows.Forms.DataGridViewCell)
  38. If value IsNot Nothing AndAlso Not value.GetType().IsAssignableFrom(GetType(MaskedTextBoxCell)) Then
  39. Dim s As String = "Cell type is not based upon the MaskedTextBoxCell."
  40. Throw New InvalidCastException(s)
  41. End If
  42. MyBase.CellTemplate = value
  43. End Set
  44. End Property
  45. Public Overridable Property Mask() As String
  46. Get
  47. Return Me._mask
  48. End Get
  49. Set(ByVal value As String)
  50. Dim mtbCell As MaskedTextBoxCell
  51. Dim dgvCell As DataGridViewCell
  52. Dim rowCount As Integer
  53. If Me._mask <> value Then
  54. Me._mask = value
  55. '
  56. ' First, update the value on the template cell.
  57. '
  58. mtbCell = DirectCast(Me.CellTemplate, MaskedTextBoxCell)
  59. mtbCell.Mask = value
  60. '
  61. ' Now set it on all cells in other rows as well.
  62. '
  63. If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
  64. rowCount = Me.DataGridView.Rows.Count
  65. For x As Integer = 0 To rowCount - 1
  66. dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
  67. If TypeOf dgvCell Is MaskedTextBoxCell Then
  68. mtbCell = CType(dgvCell, MaskedTextBoxCell)
  69. mtbCell.Mask = value
  70. End If
  71. Next
  72. End If
  73. End If
  74. End Set
  75. End Property
  76. Public Overridable Property PromptChar() As Char
  77. Get
  78. Return Me._promptChar
  79. End Get
  80. Set(ByVal value As Char)
  81. Dim mtbCell As MaskedTextBoxCell
  82. Dim dgvCell As DataGridViewCell
  83. Dim rowCount As Integer
  84. If Me._promptChar <> value Then
  85. Me._promptChar = value
  86. '
  87. ' First, update the value on the template cell.
  88. '
  89. mtbCell = CType(Me.CellTemplate, MaskedTextBoxCell)
  90. mtbCell.PromptChar = value
  91. '
  92. ' Now set it on all cells in other rows as well.
  93. '
  94. If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
  95. rowCount = Me.DataGridView.Rows.Count
  96. For x As Integer = 0 To rowCount - 1
  97. dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
  98. If TypeOf dgvCell Is MaskedTextBoxCell Then
  99. mtbCell = CType(dgvCell, MaskedTextBoxCell)
  100. mtbCell.PromptChar = value
  101. End If
  102. Next
  103. End If
  104. End If
  105. End Set
  106. End Property
  107. Public Overridable Property IncludePrompt() As Boolean
  108. Get
  109. Return Me._includePrompt
  110. End Get
  111. Set(ByVal value As Boolean)
  112. Dim mtbc As MaskedTextBoxCell
  113. Dim dgvc As DataGridViewCell
  114. Dim rowCount As Integer
  115. If Me._includePrompt <> value Then
  116. Me._includePrompt = value
  117. '
  118. ' First, update the value on the template cell.
  119. '
  120. mtbc = CType(Me.CellTemplate, MaskedTextBoxCell)
  121. mtbc.IncludePrompt = TriBool(value)
  122. '
  123. ' Now set it on all cells in other rows as well.
  124. '
  125. If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
  126. rowCount = Me.DataGridView.Rows.Count
  127. For x As Integer = 0 To rowCount - 1
  128. dgvc = Me.DataGridView.Rows.SharedRow(x).Cells(x)
  129. If TypeOf dgvc Is MaskedTextBoxCell Then
  130. mtbc = CType(dgvc, MaskedTextBoxCell)
  131. mtbc.IncludePrompt = TriBool(value)
  132. End If
  133. Next
  134. End If
  135. End If
  136. End Set
  137. End Property
  138. Public Overridable Property IncludeLiterals() As Boolean
  139. Get
  140. Return Me._includeLiterals
  141. End Get
  142. Set(ByVal value As Boolean)
  143. Dim mtbCell As MaskedTextBoxCell
  144. Dim dgvCell As DataGridViewCell
  145. Dim rowCount As Integer
  146. If Me._includeLiterals <> value Then
  147. Me._includeLiterals = value
  148. '
  149. ' First, update the value on the template cell.
  150. '
  151. mtbCell = CType(Me.CellTemplate, MaskedTextBoxCell)
  152. mtbCell.IncludeLiterals = TriBool(value)
  153. '
  154. ' Now set it on all cells in other rows as well.
  155. '
  156. If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
  157. rowCount = Me.DataGridView.Rows.Count
  158. For x As Integer = 0 To rowCount - 1
  159. dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
  160. If TypeOf dgvCell Is MaskedTextBoxCell Then
  161. mtbCell = CType(dgvCell, MaskedTextBoxCell)
  162. mtbCell.IncludeLiterals = TriBool(value)
  163. End If
  164. Next
  165. End If
  166. End If
  167. End Set
  168. End Property
  169. Public Overridable Property ValidatingType() As Type
  170. Get
  171. Return Me._validatingType
  172. End Get
  173. Set(ByVal value As Type)
  174. Dim mtbCell As MaskedTextBoxCell
  175. Dim dgvCell As DataGridViewCell
  176. Dim rowCount As Integer
  177. If Me._validatingType.ToString() <> value.ToString() Then
  178. Me._validatingType = value
  179. '
  180. ' First, update the value on the template cell.
  181. '
  182. mtbCell = CType(Me.CellTemplate, MaskedTextBoxCell)
  183. mtbCell.ValidatingType = value
  184. '
  185. ' Now set it on all cells in other rows as well.
  186. '
  187. If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
  188. rowCount = Me.DataGridView.Rows.Count
  189. For x As Integer = 0 To rowCount - 1
  190. dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
  191. If TypeOf dgvCell Is MaskedTextBoxCell Then
  192. mtbCell = CType(dgvCell, MaskedTextBoxCell)
  193. mtbCell.ValidatingType = value
  194. End If
  195. Next
  196. End If
  197. End If
  198. End Set
  199. End Property
  200. End Class
  201. End Namespace