PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Visual Basic | 206 lines | 132 code | 28 blank | 46 comment | 0 complexity | 10e0ba3a6b1fd570a52df297ddb9a26c MD5 | raw file
  1'************************************* Module Header **************************************\
  2' Module Name:  MaskedTextBoxColumn.vb
  3' Project:      VBWinFormDataGridView
  4' Copyright (c) Microsoft Corporation.
  5' 
  6' This sample demonstrates the use of custom column definitions within the Windows Forms 
  7' DataGridView control.
  8' 
  9' The Employee ID, SSN, State and Zip Code columns use MaskedTextBox controls for format 
 10' and validate their input.
 11' 
 12' This source is subject to the Microsoft Public License.
 13' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 14' All other rights reserved.
 15
 16' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
 17' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 18' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 19'**********************************************************************************/
 20
 21Imports System.Text
 22
 23Namespace VBWinFormDataGridView.CustomDataGridViewColumn
 24
 25    Public Class MainForm
 26        Inherits Form
 27
 28        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 29            Dim dgvTextBoxColumn As DataGridViewTextBoxColumn
 30            Dim mtbColumn As MaskedTextBoxColumn
 31            '
 32            ' Employee name.
 33            '
 34            dgvTextBoxColumn = New DataGridViewTextBoxColumn()
 35            dgvTextBoxColumn.HeaderText = "Name"
 36            dgvTextBoxColumn.Width = 120
 37            Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
 38
 39            '
 40            ' Employee ID -- this will be of the format:
 41            ' [A-Z][0-9][0-9][0-9][0-9][0-9]
 42            '
 43            ' this is well sutied to using a MaskedTextBox column.
 44            '
 45            mtbColumn = New MaskedTextBoxColumn()
 46            mtbColumn.HeaderText = "Employee ID"
 47            mtbColumn.Mask = "L00000"
 48            mtbColumn.Width = 75
 49            Me.employeesDataGridView.Columns.Add(mtbColumn)
 50
 51            '
 52            ' [American] Social Security number, of the format:
 53            ' ###-##-####
 54            ' 
 55            mtbColumn = New MaskedTextBoxColumn()
 56            mtbColumn.HeaderText = "SSN"
 57            mtbColumn.Mask = "000-00-0000"
 58            mtbColumn.Width = 75
 59            Me.employeesDataGridView.Columns.Add(mtbColumn)
 60
 61            '
 62            ' Address
 63            '
 64            dgvTextBoxColumn = New DataGridViewTextBoxColumn()
 65            dgvTextBoxColumn.HeaderText = "Address"
 66            dgvTextBoxColumn.Width = 150
 67            Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
 68
 69            '
 70            ' City
 71            '
 72            dgvTextBoxColumn = New DataGridViewTextBoxColumn()
 73            dgvTextBoxColumn.HeaderText = "City"
 74            dgvTextBoxColumn.Width = 75
 75            Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
 76
 77            '
 78            ' State
 79            '
 80            mtbColumn = New MaskedTextBoxColumn()
 81            mtbColumn.HeaderText = "State"
 82            mtbColumn.Mask = "LL"
 83            mtbColumn.Width = 40
 84            Me.employeesDataGridView.Columns.Add(mtbColumn)
 85
 86            '
 87            ' Zip Code #####-#### (+4 optional)
 88            '
 89            mtbColumn = New MaskedTextBoxColumn()
 90            mtbColumn.HeaderText = "Zip Code"
 91            mtbColumn.Mask = "00000-0000"
 92            mtbColumn.Width = 75
 93            mtbColumn.ValidatingType = GetType(ZipCode)
 94            Me.employeesDataGridView.Columns.Add(mtbColumn)
 95
 96            '
 97            ' Department Code
 98            '
 99            dgvTextBoxColumn = New DataGridViewTextBoxColumn()
100            dgvTextBoxColumn.HeaderText = "Department"
101            dgvTextBoxColumn.ValueType = GetType(Integer)
102            dgvTextBoxColumn.Width = 75
103            Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
104        End Sub
105    End Class
106
107#Region "ZipCode Class"
108    Public Class ZipCode
109        Private zipCode As Integer
110        Private plusFour As Integer
111
112        Public Sub New()
113            Me.zipCode = 0
114            Me.plusFour = 0
115        End Sub
116
117        Public Sub New(ByVal in_zipCode As String)
118
119        End Sub
120
121        Public Sub New(ByVal in_ivalue)
122            Me.zipCode = in_ivalue
123            Me.plusFour = 0
124        End Sub
125
126        Public Shared Narrowing Operator CType(ByVal s As String) As ZipCode
127            Return New ZipCode(s)
128        End Operator
129
130        Public Shared Narrowing Operator CType(ByVal i As Integer) As ZipCode
131            Return New ZipCode(i)
132        End Operator
133
134        Protected Shared Sub parseFromString(ByVal in_string As String, ByRef out_zipCode As Integer, _
135                                             ByRef out_plusFour As Integer)
136            Dim zc As Integer = 0, pf = 0
137            Dim charray As Char()
138            Dim x As Integer = 0
139            If in_string Is Nothing OrElse in_string.Equals("") Then
140                Throw New ArgumentException("Invalid String")
141            End If
142
143            charray = in_string.Trim().ToCharArray()
144
145            For x = 0 To 4
146                If Not (Char.IsDigit(charray(x))) Then
147                    Throw New ArgumentException("Invalid String")
148                End If
149                zc = zc * 10 + numfromchar(charray(x))
150            Next
151
152            While x < charray.Length AndAlso Not (Char.IsDigit(charray(x)))
153                x = x + 1
154            End While
155
156            If x < charray.Length Then
157                For y As Integer = x To 3
158                    If Not (Char.IsDigit(charray(y))) Then
159                        Throw New ArgumentException("Invalid ZipCode String")
160                    End If
161                    pf = pf * 10 + numfromchar(charray(y))
162                Next
163            End If
164
165            out_zipCode = zc
166            out_plusFour = pf
167        End Sub
168
169        Private Shared Function numfromchar(ByVal c As Char)
170            Select Case c
171                Case "0"c
172                    Return 0
173                Case "1"c
174                    Return 1
175                Case "2"c
176                    Return 2
177                Case "3"c
178                    Return 3
179                Case "4"c
180                    Return 4
181                Case "5"c
182                    Return 5
183                Case "6"c
184                    Return 6
185                Case "7"c
186                    Return 7
187                Case "8"c
188                    Return 8
189                Case "9"c
190                    Return 9
191                Case Else
192                    Throw New ArgumentException("invalid digit")
193            End Select
194        End Function
195
196        Public Overrides Function ToString() As String
197            Dim sb As StringBuilder = New StringBuilder(10)
198            sb.Append(zipCode.ToString("00000"))
199            sb.Append("_")
200            sb.Append(plusFour.ToString("0000"))
201            Return sb.ToString()
202        End Function
203    End Class
204#End Region
205
206End Namespace