PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 1ms 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
 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 MaskedTextBoxEditingControl
 20        Inherits MaskedTextBox
 21        Implements IDataGridViewEditingControl
 22
 23        Private rowIndex As Integer
 24        Private dataGridView As DataGridView
 25        Private valueChanged As Boolean = False
 26
 27        Public Sub New()
 28
 29        End Sub
 30
 31        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
 32            MyBase.OnTextChanged(e)
 33            ' Let the DataGridView know about the value change
 34            NotifyDataGridViewOfValueChange()
 35        End Sub
 36
 37        Protected Sub NotifyDataGridViewOfValueChange()
 38            Me.valueChanged = True
 39            If Me.dataGridView IsNot Nothing Then
 40                Me.dataGridView.NotifyCurrentCellDirty(True)
 41            End If
 42        End Sub
 43
 44#Region "IDataGridViewEditingControl Members"
 45        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
 46            Me.Font = dataGridViewCellStyle.Font
 47            Me.ForeColor = dataGridViewCellStyle.ForeColor
 48            Me.BackColor = dataGridViewCellStyle.BackColor
 49            Me.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment)
 50        End Sub
 51
 52        Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
 53            Get
 54                Return Me.dataGridView
 55            End Get
 56            Set(ByVal value As System.Windows.Forms.DataGridView)
 57                Me.dataGridView = value
 58            End Set
 59        End Property
 60
 61        Public Property EditingControlFormattedValue() As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
 62            Get
 63                Return Me.Text
 64            End Get
 65            Set(ByVal value As Object)
 66                Me.Text = value.ToString()
 67            End Set
 68        End Property
 69
 70        Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
 71            Get
 72                Return Me.rowIndex
 73            End Get
 74            Set(ByVal value As Integer)
 75                Me.rowIndex = value
 76            End Set
 77        End Property
 78
 79        Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
 80            Get
 81
 82            End Get
 83            Set(ByVal value As Boolean)
 84
 85            End Set
 86        End Property
 87
 88        Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
 89            Select Case keyData And Keys.KeyCode
 90                Case Keys.Right
 91                    If Not (Me.SelectionLength = 0 AndAlso Me.SelectionStart = Me.ToString().Length) Then
 92                        Return True
 93                    End If
 94                    Exit Select
 95
 96                Case Keys.Left
 97                    If Not (Me.SelectionLength = 0 AndAlso Me.SelectionStart = 0) Then
 98                        Return True
 99                    End If
100                    Exit Select
101
102                Case Keys.Home
103                Case Keys.End
104                    If Me.SelectionLength <> Me.ToString().Length Then
105                        Return True
106                    End If
107                    Exit Select
108
109                Case Keys.Prior
110                Case Keys.Next
111                    If Me.valueChanged Then
112                        Return True
113                    End If
114                    Exit Select
115
116                Case Keys.Delete
117                    If Me.SelectionLength > 0 OrElse Me.SelectionStart < Me.ToString().Length Then
118                        Return True
119                    End If
120                    Exit Select
121            End Select
122
123            Return Not dataGridViewWantsInputKey
124        End Function
125
126        Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
127            Get
128                Return Cursors.IBeam
129            End Get
130        End Property
131
132        Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
133            Return Me.Text
134        End Function
135
136        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
137            If selectAll Then
138                Me.SelectAll()
139            Else
140                Me.SelectionStart = Me.ToString().Length
141            End If
142        End Sub
143
144        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
145            Get
146                Return False
147            End Get
148        End Property
149#End Region
150
151        Private Shared Function translateAlignment(ByVal align As DataGridViewContentAlignment) As HorizontalAlignment
152            Select Case align
153                Case DataGridViewContentAlignment.TopLeft, _
154                DataGridViewContentAlignment.MiddleLeft, DataGridViewContentAlignment.BottomLeft
155                    Return HorizontalAlignment.Left
156
157                Case DataGridViewContentAlignment.TopCenter, _
158                DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.BottomCenter
159                    Return HorizontalAlignment.Center
160
161                Case DataGridViewContentAlignment.TopRight, _
162                DataGridViewContentAlignment.MiddleRight, DataGridViewContentAlignment.BottomRight
163                    Return HorizontalAlignment.Right
164            End Select
165            Throw New ArgumentException("Error: Invalid Content Alignment!")
166        End Function
167    End Class
168
169End Namespace