hello to all, I found in Internet the following code that is very usefull to me. The code detects any character that is entered to datagrid, I am interested in the control key F2 but I don't know how to adding an event handler to a column of datagrid that raise when this key is pressed. Please help m
Private Sub AddCustomDataTa bleStyle(
Dim ts1 As New DataGridTableSt yle(
ts1.MappingName = "Customers
' Add textbox column style so we can catch textbox mouse click
Dim TextCol As New DataGridKeyTrap TextBoxColum
TextCol.Mapping Name = "custID
TextCol.HeaderT ext = "CustomerID
TextCol.Width = 10
ts1.GridColumnS tyles.Add(TextC ol
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''add handler (help)''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''
DataGridKeyTrap TextBoxColumn._ RowCount = myDataSet.Table s("Customers"). Rows.Coun
ts1.GridColumnS tyles.Add(TextC ol
dataGrid1.Table Styles.Add(ts1
End Sub 'AddCustomDataT ableStyl
Public Class DataGridKeyTrap TextBoxColum
Inherits DataGridTextBox Colum
Private _keyTrapTextBox As KeyTrapTextBox = Nothin
Private _source As System.Windows. Forms.CurrencyM anager = Nothin
Private _rowNum As Intege
Private _isEditing As Boolean = Fals
Public Shared _RowCount As Integer =
Public Sub New(
_keyTrapTextBox = New KeyTrapTextBo
_keyTrapTextBox .BorderStyle = BorderStyle.Non
AddHandler _keyTrapTextBox .Leave, AddressOf LeaveKeyTrapTex tBo
AddHandler _keyTrapTextBox .KeyPress, AddressOf TextBoxEditStar te
End Sub 'Ne
Private Sub TextBoxEditStar ted(ByVal sender As Object, ByVal e As KeyPressEventAr gs
_isEditing = Tru
MyBase.ColumnSt artedEditing(CT ype(sender, Control)
End Sub 'TextBoxEditSta rte
Private Sub LeaveKeyTrapTex tBox(ByVal sender As Object, ByVal e As EventArgs
If _isEditing The
SetColumnValueA tRow(_source, _rowNum, _keyTrapTextBox .Text
_isEditing = Fals
Invalidate(
End I
_keyTrapTextBox .Hide(
End Sub 'LeaveKeyTrapTe xtBo
Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean
_RowCount = [source].Coun
MyBase.Edit([source], rowNum, bounds, [readOnly], instantText, cellIsVisible
_rowNum = rowNu
_source = [source
_keyTrapTextBox .Parent = Me.TextBox.Pare n
_keyTrapTextBox .Location = Me.TextBox.Loca tio
_keyTrapTextBox .Size = Me.TextBox.Siz
_keyTrapTextBox .Text = Me.TextBox.Tex
Me.TextBox.Visi ble = Fals
_keyTrapTextBox .Visible = Tru
_keyTrapTextBox .BringToFront(
_keyTrapTextBox .Focus(
End Sub 'Edi
Protected Overrides Function Commit(ByVal dataSource As System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer) As Boolea
If _isEditing The
_isEditing = Fals
SetColumnValueA tRow(dataSource , rowNum, _keyTrapTextBox .Text
End I
Return Tru
End Function 'Commi
End Class 'DataGridKeyTra pTextBoxColum
Public Class KeyTrapTextBo
Inherits TextBo
Public Sub New(
End Sub 'Ne
Private Const WM_KEYDOWN As Integer = &H10
Private Const WM_KEYUP As Integer = &H10
Private Const WM_CHAR As Integer = &H10
Public Overrides Function PreProcessMessa ge(ByRef msg As Message) As Boolea
Dim keyCode As Keys = CType(msg.WPara m.ToInt32(), Keys) And Keys.KeyCod
If msg.Msg = WM_KEYDOWN The
Console.WriteLi ne(("TextBox.WM _KEYDOWN key: " + keyCode.ToStrin g())
End I
' for a datagrid, we need to eat the tab key oe else its done twic
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Tab The
'to ignore a message return true without calling baseclas
'to let the textbox handle message return false;
'don't let textbox handle tab
Return True
End If
Return MyBase.PreProce ssMessage(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.PreProce ssMessage(msg)
End Function 'PreProcessMess age
End Class 'KeyTrapTextBox