Public Class DataGridKeyTrapTextBoxColumn
Inherits DataGridTextBoxColumn
Event EvHandledTab()
Public f As Form = Nothing
Private WithEvents _keyTrapTextBox As KeyTrapTextBox = Nothing
Private _source As System.Windows.Forms.CurrencyManager = Nothing
Private _rowNum As Integer
Private _isEditing As Boolean = False
Public Shared _RowCount As Integer = 0
Public Sub New()
_keyTrapTextBox = New KeyTrapTextBox()
_keyTrapTextBox.BorderStyle = BorderStyle.None
AddHandler _keyTrapTextBox.Leave, AddressOf LeaveKeyTrapTextBox
AddHandler _keyTrapTextBox.KeyPress, AddressOf TextBoxEditStarted
End Sub 'New
Private Sub TextBoxEditStarted(ByVal sender As Object, ByVal e As KeyPressEventArgs)
_isEditing = True
MyBase.ColumnStartedEditing(CType(sender, Control))
End Sub 'TextBoxEditStarted
Private Sub LeaveKeyTrapTextBox(ByVal sender As Object, ByVal e As EventArgs)
If _isEditing Then
SetColumnValueAtRow(_source, _rowNum, _keyTrapTextBox.Text)
_isEditing = False
Invalidate()
End If
_keyTrapTextBox.Hide()
End Sub 'LeaveKeyTrapTextBox
Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
_RowCount = [source].Count
MyBase.Edit([source], rowNum, bounds, [readOnly], instantText, cellIsVisible)
_rowNum = rowNum
_source = [source]
_keyTrapTextBox.Parent = Me.TextBox.Parent
_keyTrapTextBox.Location = Me.TextBox.Location
_keyTrapTextBox.Size = Me.TextBox.Size
_keyTrapTextBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
_keyTrapTextBox.Visible = True
_keyTrapTextBox.BringToFront()
_keyTrapTextBox.Focus()
End Sub 'Edit
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
If _isEditing Then
_isEditing = False
SetColumnValueAtRow(dataSource, rowNum, _keyTrapTextBox.Text)
End If
Return True
End Function 'Commit
Private Sub _keyTrapTextBox_EvHandledTab() Handles _keyTrapTextBox.EvHandledTab
If Not IsNothing(f) Then
f.SelectNextControl(f.ActiveControl, True, True, True, True)
End If
End Sub
End Class 'DataGridKeyTrapTextBoxColumn
Public Class KeyTrapTextBox
Inherits TextBox
Event EvHandledTab()
Public Sub New()
End Sub 'New
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_CHAR As Integer = &H102
Public Overrides Function PreProcessMessage(ByRef msg As Message) As Boolean
Dim keyCode As Keys = CType(msg.WParam.ToInt32(), Keys) And Keys.KeyCode
If msg.Msg = WM_KEYDOWN Then
Console.WriteLine(("TextBox.WM_KEYDOWN key: " + keyCode.ToString()))
End If
' for a datagrid, we need to eat the tab key oe else its done twice
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Tab Then
'to ignore a message return true without calling baseclass
'to let the textbox handle message return false;
'don't let textbox handle tab
keyCode = 0
RaiseEvent EvHandledTab()
Return True
End If
Return MyBase.PreProcessMessage(msg)
' //sample handling code. This lets the textbox handle the delete
' //& preventing (for example) a delete shortcut on a menu getting it
' if((msg.Msg == WM_KEYDOWN)
' && keyCode == Keys.Delete)
' {
' //to ignore a message return true without calling baseclass
' //to let the textbox handle message return false;
'
' //let textbox handle Delete
' return false;
' }
'Return MyBase.PreProcessMessage(msg)
End Function 'PreProcessMessage
End Class 'KeyTrapTextBox