PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Visual Basic | 158 lines | 100 code | 21 blank | 37 comment | 0 complexity | 804cdd4d2a0a6faa05799f2608fcdcb5 MD5 | raw file
  1. '********************************* Module Header **********************************\
  2. ' Module Name: MaskedTextBoxCell.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. '
  12. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  13. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  14. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  15. '**********************************************************************************/
  16. Namespace VBWinFormDataGridView.CustomDataGridViewColumn
  17. Public Class MaskedTextBoxCell
  18. Inherits DataGridViewTextBoxCell
  19. Private _mask As String
  20. Private _promptChar As Char
  21. Private _includePrompt As DataGridViewTriState
  22. Private _includeLiterals As DataGridViewTriState
  23. Private _validatingType As Type
  24. Public Sub New()
  25. Me._mask = ""
  26. Me._promptChar = "_"c
  27. Me._includePrompt = DataGridViewTriState.NotSet
  28. Me._includeLiterals = DataGridViewTriState.NotSet
  29. Me._validatingType = GetType(String)
  30. End Sub
  31. Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
  32. ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
  33. Dim mtbEditingCtrl As MaskedTextBoxEditingControl
  34. Dim mtbColumn As MaskedTextBoxColumn
  35. Dim dgvColumn As DataGridViewColumn
  36. MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
  37. dataGridViewCellStyle)
  38. mtbEditingCtrl = CType(DataGridView.EditingControl, MaskedTextBoxEditingControl)
  39. '
  40. ' Set up props that are specific to the MaskedTextBox
  41. '
  42. dgvColumn = Me.OwningColumn ' this.DataGridView.Columns[this.ColumnIndex];
  43. If TypeOf dgvColumn Is MaskedTextBoxColumn Then
  44. mtbColumn = CType(dgvColumn, MaskedTextBoxColumn)
  45. End If
  46. '
  47. ' get the mask from this instance or the parent column.
  48. '
  49. If String.IsNullOrEmpty(Me._mask) Then
  50. mtbEditingCtrl.Mask = mtbColumn.Mask
  51. Else
  52. mtbEditingCtrl.Mask = Me._mask
  53. '
  54. ' Prompt char.
  55. '
  56. mtbEditingCtrl.PromptChar = Me._promptChar
  57. '
  58. ' IncludePrompt
  59. '
  60. End If
  61. If Me._includePrompt = DataGridViewTriState.NotSet Then
  62. 'mtbEditingCtrl.IncludePrompt = mtbcol.IncludePrompt
  63. Else
  64. 'mtbEditingCtrl.IncludePrompt = BoolFromTri(Me.includePrompt)
  65. '
  66. ' IncludeLiterals
  67. '
  68. End If
  69. If Me._includeLiterals = DataGridViewTriState.NotSet Then
  70. 'mtbEditingCtrl.IncludeLiterals = mtbcol.IncludeLiterals
  71. Else
  72. 'mtbEditingCtrl.IncludeLiterals = BoolFromTri(Me.includeLiterals)
  73. End If
  74. '
  75. ' Finally, the validating type ...
  76. '
  77. If Me.ValidatingType Is Nothing Then
  78. mtbEditingCtrl.ValidatingType = mtbColumn.ValidatingType
  79. Else
  80. mtbEditingCtrl.ValidatingType = Me.ValidatingType
  81. mtbEditingCtrl.Text = CType(Me.Value, String)
  82. End If
  83. End Sub
  84. Public Overrides ReadOnly Property EditType() As System.Type
  85. Get
  86. Return GetType(MaskedTextBoxEditingControl)
  87. End Get
  88. End Property
  89. Public Overridable Property Mask() As String
  90. Get
  91. Return Me._mask
  92. End Get
  93. Set(ByVal value As String)
  94. Me._mask = value
  95. End Set
  96. End Property
  97. Public Overridable Property PromptChar() As Char
  98. Get
  99. Return Me._promptChar
  100. End Get
  101. Set(ByVal value As Char)
  102. Me._promptChar = value
  103. End Set
  104. End Property
  105. Public Overridable Property IncludePrompt() As DataGridViewTriState
  106. Get
  107. Return Me._includePrompt
  108. End Get
  109. Set(ByVal value As DataGridViewTriState)
  110. Me._includePrompt = value
  111. End Set
  112. End Property
  113. Public Overridable Property IncludeLiterals() As DataGridViewTriState
  114. Get
  115. Return Me._includeLiterals
  116. End Get
  117. Set(ByVal value As DataGridViewTriState)
  118. Me._includeLiterals = value
  119. End Set
  120. End Property
  121. Public Overridable Property ValidatingType() As Type
  122. Get
  123. Return Me._validatingType
  124. End Get
  125. Set(ByVal value As Type)
  126. Me._validatingType = value
  127. End Set
  128. End Property
  129. Protected Shared Function BoolFromTri(ByVal tri As DataGridViewTriState) As Boolean
  130. If tri = DataGridViewTriState.True Then
  131. Return True
  132. Else
  133. Return False
  134. End If
  135. End Function
  136. End Class
  137. End Namespace