gregasm
asked on
How does one consitently catch the .NET winForms datagrid keypress event?
I cannot seem to catch the keypress event of the .NET datagrid consistently. I am implementing search functionality for the datagrid. The search function works for the first column of the datagrid, even if a whole row is selected. When a user starts typing, the grid automatically scrolls down to the first row of the grid whose first column item begins with the same letter that was typed, and as the user types more, the grid continues scrolling down, narrowing the search.
For instance, the first column of the grid's rows are:
Alabama
Alaska
Arkansas
Autism
Imagine there is a buffer that stores the user's kepresses.
When the user types 'A', The Alabama row is highlighted, and the buffer contains 'A'. As the user continues to type 'L', then 'A', Alabama is still highlighted; the buffer is 'ALA' and when the user presses 'S' next, Alaska is highlighted and the buffer is 'ALAS'. Hopefully you are still with me at this point. Then the user presses the delete key, and the buffer is 'ALA' and alaska is still highlighted...
The code to implement this search is easy. I have written this code in the kepress event of the grid, and I would like to use the grid's captionText as the buffer. So when the user is typing on the grid, the keypress event adds and deletes from the string that is stored in the caption text. The caption text serves two purposes here: To tell the grid what is the search string, and also to visibly represent the search string to the user.
Private Sub datagrid_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPr essEventAr gs) Handles datagrid.KeyPress
e.Handled = True
Dim str As String = Microsoft.VisualBasic.ChrW (Microsoft .VisualBas ic.AscW(e. KeyChar))
If str = Microsoft.VisualBasic.ChrW (Keys.Back ).ToString Then ' handle backspace key here...
If datagrid.captiontext.lengt h < 1 Then Exit Sub
datagrid.captiontext = datagrid.captiontext.Subst ring(0, datagrid.captiontext.lengt h - 1)
doSearch(datagrid.captiont ext)
Exit Sub
End If
datagrid.captiontext &= str
doSearch(datagrid.captionT ext)
End Sub
Private Sub doSearch(ByVal srchStr As String)
If srchStr.Length = 0 Then Exit Sub
Dim i As Integer
For i = 0 To CType(gridOrders.DataSourc e, DataTable).Rows.Count - 1
If Not CType(gridOrders(i, 0), String).Length < srchStr.Length Then
If CType(gridOrders(i, 0), String).Substring(0, srchStr.Length).ToUpper = srchStr.ToUpper Then
gridOrders.CurrentCell = New DataGridCell(i, 0)
gridOrders.Select(i)
Exit Sub
End If
End If
Next
End Sub
The problem with this logic is... nothing! As far as I know. It works. problem is, when a row is selected, the grid does not fire any more keypress events until a nether region of the grid is selected (such as the very top left cell where there is no data, right up in the corner). I've tried using a label object and positioning it out of view, and everytime the keypress routine fires, the label receives focus afterwards, and trying to use the keypress event of the label. That doesn't seem to work either.
Then I thought I got smart and used the keypress event of the form itself, by setting keypreview = true. That doesn't work either because the keypress event of the form also does not fire consistently.
Any help on this issue would be greatly appreciated, and respected. I would like to thank the geniuses who frequent Experts exchange in advance for their solutions to my little riddle here.
-Greg
For instance, the first column of the grid's rows are:
Alabama
Alaska
Arkansas
Autism
Imagine there is a buffer that stores the user's kepresses.
When the user types 'A', The Alabama row is highlighted, and the buffer contains 'A'. As the user continues to type 'L', then 'A', Alabama is still highlighted; the buffer is 'ALA' and when the user presses 'S' next, Alaska is highlighted and the buffer is 'ALAS'. Hopefully you are still with me at this point. Then the user presses the delete key, and the buffer is 'ALA' and alaska is still highlighted...
The code to implement this search is easy. I have written this code in the kepress event of the grid, and I would like to use the grid's captionText as the buffer. So when the user is typing on the grid, the keypress event adds and deletes from the string that is stored in the caption text. The caption text serves two purposes here: To tell the grid what is the search string, and also to visibly represent the search string to the user.
Private Sub datagrid_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPr
e.Handled = True
Dim str As String = Microsoft.VisualBasic.ChrW
If str = Microsoft.VisualBasic.ChrW
If datagrid.captiontext.lengt
datagrid.captiontext = datagrid.captiontext.Subst
doSearch(datagrid.captiont
Exit Sub
End If
datagrid.captiontext &= str
doSearch(datagrid.captionT
End Sub
Private Sub doSearch(ByVal srchStr As String)
If srchStr.Length = 0 Then Exit Sub
Dim i As Integer
For i = 0 To CType(gridOrders.DataSourc
If Not CType(gridOrders(i, 0), String).Length < srchStr.Length Then
If CType(gridOrders(i, 0), String).Substring(0, srchStr.Length).ToUpper = srchStr.ToUpper Then
gridOrders.CurrentCell = New DataGridCell(i, 0)
gridOrders.Select(i)
Exit Sub
End If
End If
Next
End Sub
The problem with this logic is... nothing! As far as I know. It works. problem is, when a row is selected, the grid does not fire any more keypress events until a nether region of the grid is selected (such as the very top left cell where there is no data, right up in the corner). I've tried using a label object and positioning it out of view, and everytime the keypress routine fires, the label receives focus afterwards, and trying to use the keypress event of the label. That doesn't seem to work either.
Then I thought I got smart and used the keypress event of the form itself, by setting keypreview = true. That doesn't work either because the keypress event of the form also does not fire consistently.
Any help on this issue would be greatly appreciated, and respected. I would like to thank the geniuses who frequent Experts exchange in advance for their solutions to my little riddle here.
-Greg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Mr. Appari, I am much obliged. Thank you very much.
ASKER