PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms 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
 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
 17Namespace VBWinFormDataGridView.CustomDataGridViewColumn
 18
 19    Public Class MaskedTextBoxColumn
 20        Inherits DataGridViewTextBoxColumn
 21
 22        Private _mask As String
 23        Private _promptChar As Char
 24        Private _includePrompt As Boolean
 25        Private _includeLiterals As Boolean
 26        Private _validatingType As Type
 27
 28        Public Sub New()
 29            MyBase.CellTemplate = New MaskedTextBoxCell
 30        End Sub
 31
 32        Private Shared Function TriBool(ByVal value As Boolean) As DataGridViewTriState
 33            If value Then
 34                Return DataGridViewTriState.True
 35            Else
 36                Return DataGridViewTriState.False
 37            End If
 38        End Function
 39
 40        Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
 41            Get
 42                Return MyBase.CellTemplate
 43            End Get
 44            Set(ByVal value As System.Windows.Forms.DataGridViewCell)
 45                If value IsNot Nothing AndAlso Not value.GetType().IsAssignableFrom(GetType(MaskedTextBoxCell)) Then
 46                    Dim s As String = "Cell type is not based upon the MaskedTextBoxCell."
 47                    Throw New InvalidCastException(s)
 48                End If
 49                MyBase.CellTemplate = value
 50            End Set
 51        End Property
 52
 53        Public Overridable Property Mask() As String
 54            Get
 55                Return Me._mask
 56            End Get
 57            Set(ByVal value As String)
 58                Dim mtbCell As MaskedTextBoxCell
 59                Dim dgvCell As DataGridViewCell
 60                Dim rowCount As Integer
 61
 62                If Me._mask <> value Then
 63                    Me._mask = value
 64                    '
 65                    ' First, update the value on the template cell.
 66                    '
 67                    mtbCell = DirectCast(Me.CellTemplate, MaskedTextBoxCell)
 68                    mtbCell.Mask = value
 69                    '
 70                    ' Now set it on all cells in other rows as well.
 71                    '
 72                    If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
 73                        rowCount = Me.DataGridView.Rows.Count
 74                        For x As Integer = 0 To rowCount - 1
 75                            dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
 76                            If TypeOf dgvCell Is MaskedTextBoxCell Then
 77                                mtbCell = CType(dgvCell, MaskedTextBoxCell)
 78                                mtbCell.Mask = value
 79                            End If
 80                        Next
 81                    End If
 82                End If
 83            End Set
 84        End Property
 85
 86        Public Overridable Property PromptChar() As Char
 87            Get
 88                Return Me._promptChar
 89            End Get
 90            Set(ByVal value As Char)
 91                Dim mtbCell As MaskedTextBoxCell
 92                Dim dgvCell As DataGridViewCell
 93                Dim rowCount As Integer
 94                If Me._promptChar <> value Then
 95                    Me._promptChar = value
 96                    '
 97                    ' First, update the value on the template cell.
 98                    '
 99                    mtbCell = CType(Me.CellTemplate, MaskedTextBoxCell)
100                    mtbCell.PromptChar = value
101                    '
102                    ' Now set it on all cells in other rows as well.
103                    '
104                    If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
105                        rowCount = Me.DataGridView.Rows.Count
106                        For x As Integer = 0 To rowCount - 1
107                            dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
108                            If TypeOf dgvCell Is MaskedTextBoxCell Then
109                                mtbCell = CType(dgvCell, MaskedTextBoxCell)
110                                mtbCell.PromptChar = value
111                            End If
112                        Next
113                    End If
114                End If
115            End Set
116        End Property
117
118        Public Overridable Property IncludePrompt() As Boolean
119            Get
120                Return Me._includePrompt
121            End Get
122            Set(ByVal value As Boolean)
123                Dim mtbc As MaskedTextBoxCell
124                Dim dgvc As DataGridViewCell
125                Dim rowCount As Integer
126                If Me._includePrompt <> value Then
127                    Me._includePrompt = value
128
129                    '
130                    ' First, update the value on the template cell.
131                    '
132                    mtbc = CType(Me.CellTemplate, MaskedTextBoxCell)
133                    mtbc.IncludePrompt = TriBool(value)
134                    '
135                    ' Now set it on all cells in other rows as well.
136                    '
137                    If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
138                        rowCount = Me.DataGridView.Rows.Count
139                        For x As Integer = 0 To rowCount - 1
140                            dgvc = Me.DataGridView.Rows.SharedRow(x).Cells(x)
141                            If TypeOf dgvc Is MaskedTextBoxCell Then
142                                mtbc = CType(dgvc, MaskedTextBoxCell)
143                                mtbc.IncludePrompt = TriBool(value)
144                            End If
145                        Next
146                    End If
147                End If
148            End Set
149        End Property
150
151        Public Overridable Property IncludeLiterals() As Boolean
152            Get
153                Return Me._includeLiterals
154            End Get
155            Set(ByVal value As Boolean)
156                Dim mtbCell As MaskedTextBoxCell
157                Dim dgvCell As DataGridViewCell
158                Dim rowCount As Integer
159                If Me._includeLiterals <> value Then
160                    Me._includeLiterals = value
161                    '
162                    ' First, update the value on the template cell.
163                    '
164                    mtbCell = CType(Me.CellTemplate, MaskedTextBoxCell)
165                    mtbCell.IncludeLiterals = TriBool(value)
166                    '
167                    ' Now set it on all cells in other rows as well.
168                    '
169                    If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
170                        rowCount = Me.DataGridView.Rows.Count
171                        For x As Integer = 0 To rowCount - 1
172                            dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
173                            If TypeOf dgvCell Is MaskedTextBoxCell Then
174                                mtbCell = CType(dgvCell, MaskedTextBoxCell)
175                                mtbCell.IncludeLiterals = TriBool(value)
176                            End If
177                        Next
178                    End If
179                End If
180            End Set
181        End Property
182
183        Public Overridable Property ValidatingType() As Type
184            Get
185                Return Me._validatingType
186            End Get
187            Set(ByVal value As Type)
188                Dim mtbCell As MaskedTextBoxCell
189                Dim dgvCell As DataGridViewCell
190                Dim rowCount As Integer
191                If Me._validatingType.ToString() <> value.ToString() Then
192                    Me._validatingType = value
193                    '
194                    ' First, update the value on the template cell.
195                    '
196                    mtbCell = CType(Me.CellTemplate, MaskedTextBoxCell)
197                    mtbCell.ValidatingType = value
198                    '
199                    ' Now set it on all cells in other rows as well.
200                    '
201                    If Me.DataGridView IsNot Nothing AndAlso Me.DataGridView.Rows IsNot Nothing Then
202                        rowCount = Me.DataGridView.Rows.Count
203                        For x As Integer = 0 To rowCount - 1
204                            dgvCell = Me.DataGridView.Rows.SharedRow(x).Cells(x)
205                            If TypeOf dgvCell Is MaskedTextBoxCell Then
206                                mtbCell = CType(dgvCell, MaskedTextBoxCell)
207                                mtbCell.ValidatingType = value
208                            End If
209                        Next
210                    End If
211                End If
212            End Set
213        End Property
214
215    End Class
216
217End Namespace