Event Handler for Control Key F2

G

Guest

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 AddCustomDataTableStyle(
Dim ts1 As New DataGridTableStyle(
ts1.MappingName = "Customers
' Add textbox column style so we can catch textbox mouse click
Dim TextCol As New DataGridKeyTrapTextBoxColum
TextCol.MappingName = "custID
TextCol.HeaderText = "CustomerID
TextCol.Width = 10
ts1.GridColumnStyles.Add(TextCol

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''add handler (help)''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

DataGridKeyTrapTextBoxColumn._RowCount = myDataSet.Tables("Customers").Rows.Coun
ts1.GridColumnStyles.Add(TextCol
dataGrid1.TableStyles.Add(ts1
End Sub 'AddCustomDataTableStyl

Public Class DataGridKeyTrapTextBoxColum
Inherits DataGridTextBoxColum
Private _keyTrapTextBox As KeyTrapTextBox = Nothin
Private _source As System.Windows.Forms.CurrencyManager = 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 LeaveKeyTrapTextBo
AddHandler _keyTrapTextBox.KeyPress, AddressOf TextBoxEditStarte
End Sub 'Ne

Private Sub TextBoxEditStarted(ByVal sender As Object, ByVal e As KeyPressEventArgs
_isEditing = Tru
MyBase.ColumnStartedEditing(CType(sender, Control)
End Sub 'TextBoxEditStarte

Private Sub LeaveKeyTrapTextBox(ByVal sender As Object, ByVal e As EventArgs
If _isEditing The
SetColumnValueAtRow(_source, _rowNum, _keyTrapTextBox.Text
_isEditing = Fals
Invalidate(
End I
_keyTrapTextBox.Hide(
End Sub 'LeaveKeyTrapTextBo

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].Coun

MyBase.Edit([source], rowNum, bounds, [readOnly], instantText, cellIsVisible

_rowNum = rowNu
_source = [source

_keyTrapTextBox.Parent = Me.TextBox.Paren
_keyTrapTextBox.Location = Me.TextBox.Locatio
_keyTrapTextBox.Size = Me.TextBox.Siz
_keyTrapTextBox.Text = Me.TextBox.Tex
Me.TextBox.Visible = Fals
_keyTrapTextBox.Visible = Tru
_keyTrapTextBox.BringToFront(
_keyTrapTextBox.Focus(
End Sub 'Edi

Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolea
If _isEditing The
_isEditing = Fals
SetColumnValueAtRow(dataSource, rowNum, _keyTrapTextBox.Text
End I
Return Tru
End Function 'Commi
End Class 'DataGridKeyTrapTextBoxColum

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 PreProcessMessage(ByRef msg As Message) As Boolea
Dim keyCode As Keys = CType(msg.WParam.ToInt32(), Keys) And Keys.KeyCod
If msg.Msg = WM_KEYDOWN The
Console.WriteLine(("TextBox.WM_KEYDOWN key: " + keyCode.ToString())
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.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
 
K

Ken Tucker [MVP]

Hi,

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q836q

Ken
---------------
Carmen de Lara said:
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 me

Private Sub AddCustomDataTableStyle()
Dim ts1 As New DataGridTableStyle()
ts1.MappingName = "Customers"
' Add textbox column style so we can catch textbox mouse
clicks
Dim TextCol As New DataGridKeyTrapTextBoxColumn
TextCol.MappingName = "custID"
TextCol.HeaderText = "CustomerID"
TextCol.Width = 100
ts1.GridColumnStyles.Add(TextCol)


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''add
handler
(help)'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

DataGridKeyTrapTextBoxColumn._RowCount =
myDataSet.Tables("Customers").Rows.Count
ts1.GridColumnStyles.Add(TextCol)
dataGrid1.TableStyles.Add(ts1)
End Sub 'AddCustomDataTableStyle

Public Class DataGridKeyTrapTextBoxColumn
Inherits DataGridTextBoxColumn
Private _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
End Class 'DataGridKeyTrapTextBoxColumn

Public Class KeyTrapTextBox
Inherits TextBox

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
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
 
G

Guest

i saw the code but i don't know how to raise an event handler to trap de F2 key control and then show a new form. Please help me
 
K

Ken Tucker [MVP]

Hi,

Here is some code. It pops us a messagebox when you press f2.
You could just as easily open a form or raise an event.

Public Class KeyTrapTextBox
Inherits TextBox

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
Return True
ElseIf msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.F2 Then
MessageBox.Show("F2")
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

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top