PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Visual Basic | 169 lines | 125 code | 29 blank | 15 comment | 0 complexity | da069b361e29a475a41e5fa4137b6d65 MD5 | raw file
  1. '********************************* Module Header **********************************\
  2. ' Module Name: MaskedTextBoxEditingControl.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. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  12. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  13. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  14. '**********************************************************************************/
  15. Namespace VBWinFormDataGridView.CustomDataGridViewColumn
  16. Public Class MaskedTextBoxEditingControl
  17. Inherits MaskedTextBox
  18. Implements IDataGridViewEditingControl
  19. Private rowIndex As Integer
  20. Private dataGridView As DataGridView
  21. Private valueChanged As Boolean = False
  22. Public Sub New()
  23. End Sub
  24. Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
  25. MyBase.OnTextChanged(e)
  26. ' Let the DataGridView know about the value change
  27. NotifyDataGridViewOfValueChange()
  28. End Sub
  29. Protected Sub NotifyDataGridViewOfValueChange()
  30. Me.valueChanged = True
  31. If Me.dataGridView IsNot Nothing Then
  32. Me.dataGridView.NotifyCurrentCellDirty(True)
  33. End If
  34. End Sub
  35. #Region "IDataGridViewEditingControl Members"
  36. Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
  37. Me.Font = dataGridViewCellStyle.Font
  38. Me.ForeColor = dataGridViewCellStyle.ForeColor
  39. Me.BackColor = dataGridViewCellStyle.BackColor
  40. Me.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment)
  41. End Sub
  42. Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
  43. Get
  44. Return Me.dataGridView
  45. End Get
  46. Set(ByVal value As System.Windows.Forms.DataGridView)
  47. Me.dataGridView = value
  48. End Set
  49. End Property
  50. Public Property EditingControlFormattedValue() As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
  51. Get
  52. Return Me.Text
  53. End Get
  54. Set(ByVal value As Object)
  55. Me.Text = value.ToString()
  56. End Set
  57. End Property
  58. Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
  59. Get
  60. Return Me.rowIndex
  61. End Get
  62. Set(ByVal value As Integer)
  63. Me.rowIndex = value
  64. End Set
  65. End Property
  66. Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
  67. Get
  68. End Get
  69. Set(ByVal value As Boolean)
  70. End Set
  71. End Property
  72. Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
  73. Select Case keyData And Keys.KeyCode
  74. Case Keys.Right
  75. If Not (Me.SelectionLength = 0 AndAlso Me.SelectionStart = Me.ToString().Length) Then
  76. Return True
  77. End If
  78. Exit Select
  79. Case Keys.Left
  80. If Not (Me.SelectionLength = 0 AndAlso Me.SelectionStart = 0) Then
  81. Return True
  82. End If
  83. Exit Select
  84. Case Keys.Home
  85. Case Keys.End
  86. If Me.SelectionLength <> Me.ToString().Length Then
  87. Return True
  88. End If
  89. Exit Select
  90. Case Keys.Prior
  91. Case Keys.Next
  92. If Me.valueChanged Then
  93. Return True
  94. End If
  95. Exit Select
  96. Case Keys.Delete
  97. If Me.SelectionLength > 0 OrElse Me.SelectionStart < Me.ToString().Length Then
  98. Return True
  99. End If
  100. Exit Select
  101. End Select
  102. Return Not dataGridViewWantsInputKey
  103. End Function
  104. Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
  105. Get
  106. Return Cursors.IBeam
  107. End Get
  108. End Property
  109. Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
  110. Return Me.Text
  111. End Function
  112. Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
  113. If selectAll Then
  114. Me.SelectAll()
  115. Else
  116. Me.SelectionStart = Me.ToString().Length
  117. End If
  118. End Sub
  119. Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
  120. Get
  121. Return False
  122. End Get
  123. End Property
  124. #End Region
  125. Private Shared Function translateAlignment(ByVal align As DataGridViewContentAlignment) As HorizontalAlignment
  126. Select Case align
  127. Case DataGridViewContentAlignment.TopLeft, _
  128. DataGridViewContentAlignment.MiddleLeft, DataGridViewContentAlignment.BottomLeft
  129. Return HorizontalAlignment.Left
  130. Case DataGridViewContentAlignment.TopCenter, _
  131. DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.BottomCenter
  132. Return HorizontalAlignment.Center
  133. Case DataGridViewContentAlignment.TopRight, _
  134. DataGridViewContentAlignment.MiddleRight, DataGridViewContentAlignment.BottomRight
  135. Return HorizontalAlignment.Right
  136. End Select
  137. Throw New ArgumentException("Error: Invalid Content Alignment!")
  138. End Function
  139. End Class
  140. End Namespace