PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  16. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  17. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  18. '**********************************************************************************/
  19. Imports System.Text
  20. Namespace VBWinFormDataGridView.CustomDataGridViewColumn
  21. Public Class MainForm
  22. Inherits Form
  23. Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  24. Dim dgvTextBoxColumn As DataGridViewTextBoxColumn
  25. Dim mtbColumn As MaskedTextBoxColumn
  26. '
  27. ' Employee name.
  28. '
  29. dgvTextBoxColumn = New DataGridViewTextBoxColumn()
  30. dgvTextBoxColumn.HeaderText = "Name"
  31. dgvTextBoxColumn.Width = 120
  32. Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
  33. '
  34. ' Employee ID -- this will be of the format:
  35. ' [A-Z][0-9][0-9][0-9][0-9][0-9]
  36. '
  37. ' this is well sutied to using a MaskedTextBox column.
  38. '
  39. mtbColumn = New MaskedTextBoxColumn()
  40. mtbColumn.HeaderText = "Employee ID"
  41. mtbColumn.Mask = "L00000"
  42. mtbColumn.Width = 75
  43. Me.employeesDataGridView.Columns.Add(mtbColumn)
  44. '
  45. ' [American] Social Security number, of the format:
  46. ' ###-##-####
  47. '
  48. mtbColumn = New MaskedTextBoxColumn()
  49. mtbColumn.HeaderText = "SSN"
  50. mtbColumn.Mask = "000-00-0000"
  51. mtbColumn.Width = 75
  52. Me.employeesDataGridView.Columns.Add(mtbColumn)
  53. '
  54. ' Address
  55. '
  56. dgvTextBoxColumn = New DataGridViewTextBoxColumn()
  57. dgvTextBoxColumn.HeaderText = "Address"
  58. dgvTextBoxColumn.Width = 150
  59. Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
  60. '
  61. ' City
  62. '
  63. dgvTextBoxColumn = New DataGridViewTextBoxColumn()
  64. dgvTextBoxColumn.HeaderText = "City"
  65. dgvTextBoxColumn.Width = 75
  66. Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
  67. '
  68. ' State
  69. '
  70. mtbColumn = New MaskedTextBoxColumn()
  71. mtbColumn.HeaderText = "State"
  72. mtbColumn.Mask = "LL"
  73. mtbColumn.Width = 40
  74. Me.employeesDataGridView.Columns.Add(mtbColumn)
  75. '
  76. ' Zip Code #####-#### (+4 optional)
  77. '
  78. mtbColumn = New MaskedTextBoxColumn()
  79. mtbColumn.HeaderText = "Zip Code"
  80. mtbColumn.Mask = "00000-0000"
  81. mtbColumn.Width = 75
  82. mtbColumn.ValidatingType = GetType(ZipCode)
  83. Me.employeesDataGridView.Columns.Add(mtbColumn)
  84. '
  85. ' Department Code
  86. '
  87. dgvTextBoxColumn = New DataGridViewTextBoxColumn()
  88. dgvTextBoxColumn.HeaderText = "Department"
  89. dgvTextBoxColumn.ValueType = GetType(Integer)
  90. dgvTextBoxColumn.Width = 75
  91. Me.employeesDataGridView.Columns.Add(dgvTextBoxColumn)
  92. End Sub
  93. End Class
  94. #Region "ZipCode Class"
  95. Public Class ZipCode
  96. Private zipCode As Integer
  97. Private plusFour As Integer
  98. Public Sub New()
  99. Me.zipCode = 0
  100. Me.plusFour = 0
  101. End Sub
  102. Public Sub New(ByVal in_zipCode As String)
  103. End Sub
  104. Public Sub New(ByVal in_ivalue)
  105. Me.zipCode = in_ivalue
  106. Me.plusFour = 0
  107. End Sub
  108. Public Shared Narrowing Operator CType(ByVal s As String) As ZipCode
  109. Return New ZipCode(s)
  110. End Operator
  111. Public Shared Narrowing Operator CType(ByVal i As Integer) As ZipCode
  112. Return New ZipCode(i)
  113. End Operator
  114. Protected Shared Sub parseFromString(ByVal in_string As String, ByRef out_zipCode As Integer, _
  115. ByRef out_plusFour As Integer)
  116. Dim zc As Integer = 0, pf = 0
  117. Dim charray As Char()
  118. Dim x As Integer = 0
  119. If in_string Is Nothing OrElse in_string.Equals("") Then
  120. Throw New ArgumentException("Invalid String")
  121. End If
  122. charray = in_string.Trim().ToCharArray()
  123. For x = 0 To 4
  124. If Not (Char.IsDigit(charray(x))) Then
  125. Throw New ArgumentException("Invalid String")
  126. End If
  127. zc = zc * 10 + numfromchar(charray(x))
  128. Next
  129. While x < charray.Length AndAlso Not (Char.IsDigit(charray(x)))
  130. x = x + 1
  131. End While
  132. If x < charray.Length Then
  133. For y As Integer = x To 3
  134. If Not (Char.IsDigit(charray(y))) Then
  135. Throw New ArgumentException("Invalid ZipCode String")
  136. End If
  137. pf = pf * 10 + numfromchar(charray(y))
  138. Next
  139. End If
  140. out_zipCode = zc
  141. out_plusFour = pf
  142. End Sub
  143. Private Shared Function numfromchar(ByVal c As Char)
  144. Select Case c
  145. Case "0"c
  146. Return 0
  147. Case "1"c
  148. Return 1
  149. Case "2"c
  150. Return 2
  151. Case "3"c
  152. Return 3
  153. Case "4"c
  154. Return 4
  155. Case "5"c
  156. Return 5
  157. Case "6"c
  158. Return 6
  159. Case "7"c
  160. Return 7
  161. Case "8"c
  162. Return 8
  163. Case "9"c
  164. Return 9
  165. Case Else
  166. Throw New ArgumentException("invalid digit")
  167. End Select
  168. End Function
  169. Public Overrides Function ToString() As String
  170. Dim sb As StringBuilder = New StringBuilder(10)
  171. sb.Append(zipCode.ToString("00000"))
  172. sb.Append("_")
  173. sb.Append(plusFour.ToString("0000"))
  174. Return sb.ToString()
  175. End Function
  176. End Class
  177. #End Region
  178. End Namespace