Detecting keypresses in a Datagrid cell

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
 
K

Konrad L. M. Rudolph

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.
 
H

Herfried K. Wagner [MVP]

* (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
///
 
C

Cor Ligthert

Hi Herfried,

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

Cor
 
H

Herfried K. Wagner [MVP]

* "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.
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* 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.
 

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