R
Robert Styma
I am trying add keypress handling to the cells of a DataGridView without
going to "Edit mode EditProgramatically" I want to force some cells to CAPS
only, others to numerics, and some to one of several letters. I am using
visual studio 2008
I am trying to understand the descrepency between a post on another forum
and my observations.
http://social.msdn.microsoft.com/Fo.../thread/a646b4fc-143a-4e87-9c64-8d5dc0260792/
This article has the following example:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf
e.Control Is TextBox Then
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
CellKeyPress
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
CellKeyPress
End If
End Sub
Private Sub CellKeyPress(ByVal sender As Object, ByVal e As
KeyPressEventArgs)
Debug.Print(e.KeyChar)
End Sub
This implies that the keypress handler will handle the keypress for just
that cell.
Experimentation shows that once I set the handler, it fires when editing
every cell including new added rows.
Thus, using "EditMode EditOnKeystrokeOrF2", the following code seems to work.
Note that I add the handler once per display of the panel and then use the
keypress handler to pick out what column I am using.
Private HandlerAdded As Boolean = False
Private Sub DisplayClubList_EditingControlShowing(ByVal sender As
Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles
DisplayClubList.EditingControlShowing
If Me.HandlerAdded = False And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
'---add an event handler to the TextBox control---
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
TextBox_KeyPress
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
Me.HandlerAdded = True
End If
End Sub
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If Me.DisplayClubList.CurrentCell.ColumnIndex =
DisplayClubList.Columns("ClubCode").Index Then
e.KeyChar = e.KeyChar.ToString.ToUpper
End If
End Sub
My question is: Is the technique I am using in my code the way it is
supposed to work or am I coding to an anomoly?
Also, is this behavior consistant between .net 2.0 and .net 3.5?
going to "Edit mode EditProgramatically" I want to force some cells to CAPS
only, others to numerics, and some to one of several letters. I am using
visual studio 2008
I am trying to understand the descrepency between a post on another forum
and my observations.
http://social.msdn.microsoft.com/Fo.../thread/a646b4fc-143a-4e87-9c64-8d5dc0260792/
This article has the following example:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf
e.Control Is TextBox Then
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
CellKeyPress
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
CellKeyPress
End If
End Sub
Private Sub CellKeyPress(ByVal sender As Object, ByVal e As
KeyPressEventArgs)
Debug.Print(e.KeyChar)
End Sub
This implies that the keypress handler will handle the keypress for just
that cell.
Experimentation shows that once I set the handler, it fires when editing
every cell including new added rows.
Thus, using "EditMode EditOnKeystrokeOrF2", the following code seems to work.
Note that I add the handler once per display of the panel and then use the
keypress handler to pick out what column I am using.
Private HandlerAdded As Boolean = False
Private Sub DisplayClubList_EditingControlShowing(ByVal sender As
Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles
DisplayClubList.EditingControlShowing
If Me.HandlerAdded = False And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
'---add an event handler to the TextBox control---
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
TextBox_KeyPress
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
Me.HandlerAdded = True
End If
End Sub
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If Me.DisplayClubList.CurrentCell.ColumnIndex =
DisplayClubList.Columns("ClubCode").Index Then
e.KeyChar = e.KeyChar.ToString.ToUpper
End If
End Sub
My question is: Is the technique I am using in my code the way it is
supposed to work or am I coding to an anomoly?
Also, is this behavior consistant between .net 2.0 and .net 3.5?