Detecting keypresses in a Datagrid cell

  • Thread starter Thread starter robert_rowe
  • Start date Start date
R

robert_rowe

Does anyone know how to detect keypresses in a Datagrid cell? The
Keypress event ofthe datagrid doesn't fire if you are in a cell.
Neither does KeyUp & KeyDown. I've tried hooking in to the
DataGridCell events but it doesn't have the keypress event.

Robert
 
robert_rowe said:
Does anyone know how to detect keypresses in a Datagrid cell? The
Keypress event ofthe datagrid doesn't fire if you are in a cell.
Neither does KeyUp & KeyDown. I've tried hooking in to the
DataGridCell events but it doesn't have the keypress event.

Guess:
set the KeyPreview property of the parent form to "True" and use the
KeyPress event of the form instead.
 
* (e-mail address removed)-spam.invalid (robert_rowe) scripsit:
Does anyone know how to detect keypresses in a Datagrid cell? The
Keypress event ofthe datagrid doesn't fire if you are in a cell.
Neither does KeyUp & KeyDown. I've tried hooking in to the
DataGridCell events but it doesn't have the keypress event.

You will have to take the 'KeyPress' event of the cell. In order to do
that, you will have to derive a class from 'DataGridColumnStyle' and
assign your own 'GridColumnStyle' of your 'TableStyle'. Then you add a
handler to the 'KeyPress' event of the class created in the last step.

Source code (taken from a post by Peter Fleischer):

\\\
....
Me.DataGrid1.TableStyles.Clear()
Dim dgts As New DataGridTableStyle
dgts.MappingName = "Tab1"
Me.DataGrid1.TableStyles.Add(dgts)
Dim myTextBoxColumn As DataGridTextBoxColumn = _
DataGrid1.TableStyles("Tab1").GridColumnStyles("Feld2")
Dim dgtb As DataGridTextBox = _
CType(myTextBoxColumn.TextBox, DataGridTextBox)
AddHandler dgtb.KeyPress, AddressOf meineTastenPresse
....

Private Sub meineTastenPresse(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs)
Debug.WriteLine("Gedrückt: " & Asc(e.KeyChar).ToString)
End Sub
///
 
Hi Herfried,

Is there a reason why you use CType instead of DirectCast in this sample?

Cor
 
* "Cor Ligthert said:
Is there a reason why you use CType instead of DirectCast in this sample?

It's Peter Fleischer's example, not mine ;-). I didn't change anything
in the code.
 
Hi Herfried,

I mean this one that I made some days ago for OHM, this sample is complete
and in the line of this newsgroup, I did not know there exist already one.
(Do not cry there are also with HKW in it).

http://groups.google.com/groups?selm=#[email protected]

It is valuechanged in stead of keypresses of course because you says always
that that is better
(not serious)

And that is the reason I saw your CType because I had used it there, I have
changed it now in my snippets in my HKW database, which is by the way a
tooltip sample in a datagrid that I changed for OHM.

Cor
 
* Cor Ligthert:
So why his and not mine?

I did not copied mine from his.

I didn't see that you posted a sample, and I would have had to change
it.
 
Back
Top