PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms 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. Namespace VBWinFormDataGridView.EditingControlHosting
  18. Public Class MainForm
  19. Private maskedTextBoxForEditing As MaskedTextBox
  20. Private IsKeyPressHandled As Boolean
  21. Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  22. Me.maskedTextBoxForEditing = New MaskedTextBox()
  23. ' The "000-00-0000" mask allows only digits can be input
  24. Me.maskedTextBoxForEditing.Mask = "000-00-0000"
  25. ' Hide the MaskedTextBox
  26. Me.maskedTextBoxForEditing.Visible = False
  27. ' Add the MaskedTextBox to the DataGridView's control collection
  28. Me.dataGridView1.Controls.Add(Me.maskedTextBoxForEditing)
  29. ' Add a DataGridViewTextBoxColumn to the
  30. Dim tc As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
  31. tc.HeaderText = "Mask Column"
  32. tc.Name = "MaskColumn"
  33. Me.dataGridView1.Columns.Add(tc)
  34. ' Add some empty rows for testing purpose
  35. For j As Integer = 0 To 29
  36. Me.dataGridView1.Rows.Add()
  37. Next
  38. ' Handle the CellBeginEdit event to show the MaskedTextBox on
  39. ' the current editing cell
  40. End Sub
  41. Private Sub dataGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dataGridView1.CellBeginEdit
  42. ' If the current cell is on the "MaskColumn", we use the MaskedTextBox control
  43. ' for editing instead of the default TextBox control
  44. If e.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index Then
  45. ' Calculate the cell bounds of the current cell
  46. Dim rect As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
  47. ' Adjust the MaskedTextBox's size and location to fit the cell
  48. Me.maskedTextBoxForEditing.Size = rect.Size
  49. Me.maskedTextBoxForEditing.Location = rect.Location
  50. ' Set value for the MaskedTextBox
  51. If Me.dataGridView1.CurrentCell.Value IsNot Nothing Then
  52. Me.maskedTextBoxForEditing.Text = Me.dataGridView1.CurrentCell.Value.ToString()
  53. End If
  54. ' Show the MaskedTextBox
  55. Me.maskedTextBoxForEditing.Visible = True
  56. End If
  57. End Sub
  58. Private Sub dataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellEndEdit
  59. ' When finish editing on the "MaskColumn", we replace the cell value with
  60. ' the text typed in the MaskedTextBox, and hide the MaskedTextBox
  61. If e.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index Then
  62. Me.dataGridView1.CurrentCell.Value = Me.maskedTextBoxForEditing.Text
  63. Me.maskedTextBoxForEditing.Text = ""
  64. Me.maskedTextBoxForEditing.Visible = False
  65. End If
  66. End Sub
  67. Private Sub dataGridView1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles dataGridView1.Scroll
  68. If Me.dataGridView1.IsCurrentCellInEditMode = True Then
  69. ' Adjust the location for the MaskedTextBox while scrolling
  70. Dim rect As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(Me.dataGridView1.CurrentCell.ColumnIndex, Me.dataGridView1.CurrentCell.RowIndex, True)
  71. Console.WriteLine(rect.ToString())
  72. Console.WriteLine(Me.dataGridView1.CurrentCellAddress.ToString())
  73. Console.WriteLine("")
  74. If rect.X <= 0 OrElse rect.Y <= 0 Then
  75. Me.maskedTextBoxForEditing.Visible = False
  76. Else
  77. Me.maskedTextBoxForEditing.Location = rect.Location
  78. End If
  79. End If
  80. End Sub
  81. Private Sub dataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dataGridView1.EditingControlShowing
  82. If Not (Me.IsKeyPressHandled AndAlso Me.dataGridView1.CurrentCell.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index) Then
  83. Dim tb As TextBox = CType(e.Control, TextBox)
  84. AddHandler tb.KeyPress, AddressOf tb_KeyPress
  85. Me.IsKeyPressHandled = True
  86. End If
  87. End Sub
  88. Private Sub tb_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
  89. If Me.dataGridView1.CurrentCell.ColumnIndex = Me.dataGridView1.Columns("MaskColumn").Index Then
  90. ' Prevent the key char to be input in the editing control
  91. e.Handled = True
  92. ' Set focus to the MaskedTextBox for editing.
  93. Me.maskedTextBoxForEditing.Focus()
  94. End If
  95. End Sub
  96. End Class
  97. End Namespace