PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms 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
 17Namespace VBWinFormDataGridView.CustomDataGridViewColumn
 18    Public Class MaskedTextBoxCell
 19        Inherits DataGridViewTextBoxCell
 20
 21        Private _mask As String
 22        Private _promptChar As Char
 23        Private _includePrompt As DataGridViewTriState
 24        Private _includeLiterals As DataGridViewTriState
 25        Private _validatingType As Type
 26
 27        Public Sub New()
 28            Me._mask = ""
 29            Me._promptChar = "_"c
 30            Me._includePrompt = DataGridViewTriState.NotSet
 31            Me._includeLiterals = DataGridViewTriState.NotSet
 32            Me._validatingType = GetType(String)
 33        End Sub
 34
 35        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
 36                ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
 37
 38            Dim mtbEditingCtrl As MaskedTextBoxEditingControl
 39            Dim mtbColumn As MaskedTextBoxColumn
 40            Dim dgvColumn As DataGridViewColumn
 41
 42            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
 43                                          dataGridViewCellStyle)
 44
 45            mtbEditingCtrl = CType(DataGridView.EditingControl, MaskedTextBoxEditingControl)
 46
 47            '
 48            ' Set up props that are specific to the MaskedTextBox
 49            '
 50
 51            dgvColumn = Me.OwningColumn   ' this.DataGridView.Columns[this.ColumnIndex];
 52            If TypeOf dgvColumn Is MaskedTextBoxColumn Then
 53
 54                mtbColumn = CType(dgvColumn, MaskedTextBoxColumn)
 55            End If
 56            '
 57            ' get the mask from this instance or the parent column.
 58            '
 59            If String.IsNullOrEmpty(Me._mask) Then
 60
 61                mtbEditingCtrl.Mask = mtbColumn.Mask
 62
 63            Else
 64                mtbEditingCtrl.Mask = Me._mask
 65                '
 66                ' Prompt char.
 67                '
 68                mtbEditingCtrl.PromptChar = Me._promptChar
 69                '
 70                ' IncludePrompt
 71                '
 72            End If
 73            If Me._includePrompt = DataGridViewTriState.NotSet Then
 74                'mtbEditingCtrl.IncludePrompt = mtbcol.IncludePrompt
 75            Else
 76                'mtbEditingCtrl.IncludePrompt = BoolFromTri(Me.includePrompt)
 77                '
 78                ' IncludeLiterals
 79                '
 80            End If
 81
 82            If Me._includeLiterals = DataGridViewTriState.NotSet Then
 83                'mtbEditingCtrl.IncludeLiterals = mtbcol.IncludeLiterals
 84            Else
 85                'mtbEditingCtrl.IncludeLiterals = BoolFromTri(Me.includeLiterals)
 86            End If
 87            '
 88            ' Finally, the validating type ...
 89            '
 90            If Me.ValidatingType Is Nothing Then
 91                mtbEditingCtrl.ValidatingType = mtbColumn.ValidatingType
 92            Else
 93                mtbEditingCtrl.ValidatingType = Me.ValidatingType
 94                mtbEditingCtrl.Text = CType(Me.Value, String)
 95            End If
 96        End Sub
 97
 98        Public Overrides ReadOnly Property EditType() As System.Type
 99            Get
100                Return GetType(MaskedTextBoxEditingControl)
101            End Get
102        End Property
103
104        Public Overridable Property Mask() As String
105            Get
106                Return Me._mask
107            End Get
108            Set(ByVal value As String)
109                Me._mask = value
110            End Set
111        End Property
112
113        Public Overridable Property PromptChar() As Char
114            Get
115                Return Me._promptChar
116            End Get
117            Set(ByVal value As Char)
118                Me._promptChar = value
119            End Set
120        End Property
121
122        Public Overridable Property IncludePrompt() As DataGridViewTriState
123            Get
124                Return Me._includePrompt
125            End Get
126            Set(ByVal value As DataGridViewTriState)
127                Me._includePrompt = value
128            End Set
129        End Property
130
131        Public Overridable Property IncludeLiterals() As DataGridViewTriState
132            Get
133                Return Me._includeLiterals
134            End Get
135            Set(ByVal value As DataGridViewTriState)
136                Me._includeLiterals = value
137            End Set
138        End Property
139
140        Public Overridable Property ValidatingType() As Type
141            Get
142                Return Me._validatingType
143            End Get
144            Set(ByVal value As Type)
145                Me._validatingType = value
146            End Set
147        End Property
148
149        Protected Shared Function BoolFromTri(ByVal tri As DataGridViewTriState) As Boolean
150            If tri = DataGridViewTriState.True Then
151                Return True
152            Else
153                Return False
154            End If
155        End Function
156    End Class
157
158End Namespace