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

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

#
Visual Basic | 115 lines | 63 code | 18 blank | 34 comment | 0 complexity | fd8a451bcfc495868b22528731c0cb1a MD5 | raw file
  1'************************************* Module Header **************************************\
  2' Module Name:  EditingControlHosting
  3' Project:      VBWinFormDataGridView
  4' Copyright (c) Microsoft Corporation.
  5' 
  6' This sample demonstrates how to host a control in the current DataGridViewCell  for 
  7' editing.
  8' 
  9' This source is subject to the Microsoft Public License.
 10' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 11' All other rights reserved.
 12'
 13' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
 14' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 15' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 16'**********************************************************************************/
 17
 18Namespace VBWinFormDataGridView.EditingControlHosting
 19
 20    Public Class MainForm
 21        Private maskedTextBoxForEditing As MaskedTextBox
 22        Private IsKeyPressHandled As Boolean
 23
 24        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 25            Me.maskedTextBoxForEditing = New MaskedTextBox()
 26
 27            ' The "000-00-0000" mask allows only digits can be input
 28            Me.maskedTextBoxForEditing.Mask = "000-00-0000"
 29
 30            ' Hide the MaskedTextBox
 31            Me.maskedTextBoxForEditing.Visible = False
 32
 33            ' Add the MaskedTextBox to the DataGridView's control collection
 34            Me.dataGridView1.Controls.Add(Me.maskedTextBoxForEditing)
 35
 36            ' Add a DataGridViewTextBoxColumn to the 
 37            Dim tc As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
 38            tc.HeaderText = "Mask Column"
 39            tc.Name = "MaskColumn"
 40            Me.dataGridView1.Columns.Add(tc)
 41
 42            ' Add some empty rows for testing purpose
 43            For j As Integer = 0 To 29
 44                Me.dataGridView1.Rows.Add()
 45            Next
 46
 47            ' Handle the CellBeginEdit event to show the MaskedTextBox on
 48            ' the current editing cell
 49        End Sub
 50
 51        Private Sub dataGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dataGridView1.CellBeginEdit
 52            ' If the current cell is on the "MaskColumn", we use the MaskedTextBox control
 53            ' for editing instead of the default TextBox control
 54            If e.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index Then
 55                ' Calculate the cell bounds of the current cell
 56                Dim rect As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
 57                ' Adjust the MaskedTextBox's size and location to fit the cell
 58                Me.maskedTextBoxForEditing.Size = rect.Size
 59                Me.maskedTextBoxForEditing.Location = rect.Location
 60                ' Set value for the MaskedTextBox
 61                If Me.dataGridView1.CurrentCell.Value IsNot Nothing Then
 62                    Me.maskedTextBoxForEditing.Text = Me.dataGridView1.CurrentCell.Value.ToString()
 63                End If
 64
 65                ' Show the MaskedTextBox
 66                Me.maskedTextBoxForEditing.Visible = True
 67            End If
 68        End Sub
 69
 70        Private Sub dataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellEndEdit
 71            ' When finish editing on the "MaskColumn", we replace the cell value with
 72            ' the text typed in the MaskedTextBox, and hide the MaskedTextBox
 73            If e.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index Then
 74                Me.dataGridView1.CurrentCell.Value = Me.maskedTextBoxForEditing.Text
 75                Me.maskedTextBoxForEditing.Text = ""
 76                Me.maskedTextBoxForEditing.Visible = False
 77            End If
 78        End Sub
 79
 80        Private Sub dataGridView1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dataGridView1.Scroll
 81            If Me.dataGridView1.IsCurrentCellInEditMode = True Then
 82                ' Adjust the location for the MaskedTextBox while scrolling
 83                Dim rect As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(Me.dataGridView1.CurrentCell.ColumnIndex, Me.dataGridView1.CurrentCell.RowIndex, True)
 84                Console.WriteLine(rect.ToString())
 85                Console.WriteLine(Me.dataGridView1.CurrentCellAddress.ToString())
 86                Console.WriteLine("")
 87
 88                If rect.X <= 0 OrElse rect.Y <= 0 Then
 89                    Me.maskedTextBoxForEditing.Visible = False
 90                Else
 91                    Me.maskedTextBoxForEditing.Location = rect.Location
 92                End If
 93            End If
 94        End Sub
 95
 96        Private Sub dataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dataGridView1.EditingControlShowing
 97            If Not (Me.IsKeyPressHandled AndAlso Me.dataGridView1.CurrentCell.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index) Then
 98                Dim tb As TextBox = CType(e.Control, TextBox)
 99                AddHandler tb.KeyPress, AddressOf tb_KeyPress
100                Me.IsKeyPressHandled = True
101            End If
102        End Sub
103
104        Private Sub tb_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
105            If Me.dataGridView1.CurrentCell.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index Then
106                ' Prevent the key char to be input in the editing control
107                e.Handled = True
108
109                ' Set focus to the MaskedTextBox for editing.
110                Me.maskedTextBoxForEditing.Focus()
111            End If
112        End Sub
113    End Class
114
115End Namespace