Public Class frm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
' This is omitted, because it'll be created when you set up the form
#End Region
Private strSearch As String ' Search string, if typing in a lease number
Private dv As DataView
Private blnBackSpace As Boolean ' User typed a backspace to shorten search string
Private Sub frm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
dv = dsA.dt.DefaultView
dv.Sort = "fldKey"
dgv.DataSource = dv
dgv.Select()
ClearSearch()
blnBackSpace = False
End Sub
Private Sub dgv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgv.KeyDown
' This event handler works with dgv_KeyPress to provide a progressive search in the DataGridView based on
' fldKey. This event handler responds to Backspace, up & down arrow, and Page Up/Page Down, which
' cannot be processed by the KeyPress event.
Dim intSearch As Integer
Select Case e.KeyCode
Case Keys.Back
If Len(strSearch) = 0 Then
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep) ' Play error bell
Else
' If character deleted was an escaped special character, remove the brackets as well
If Mid(strSearch, Len(strSearch)) = "]" Then
strSearch = Mid(strSearch, 1, Len(strSearch) - 3)
Else
strSearch = Mid(strSearch, 1, Len(strSearch) - 1)
End If
If Len(strSearch) = 0 Then
intSearch = 0
ClearSearch()
Else
' Find what's the first row that matches the new search string. We know there will be a match,
' because this is a wider search than what's currently active.
intSearch = FindMatch()
lblSearchText.Text = Mid(lblSearchText.Text, 1, Len(lblSearchText.Text) - 1)
End If
dgv.CurrentCell = dgv.Item(0, intSearch) ' *** This assumes fldKey is the first column of the row
End If
blnBackSpace = True
Case Keys.Down, Keys.Up, Keys.PageDown, Keys.PageUp
' If we use one of these keys, cancel the search process
ClearSearch()
End Select
End Sub
Private Sub dgv_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgv.KeyPress
' Progressive search for lease number. Add each character typed to the search string and look for a match
' The current search string is displayed in lblSearchDisplay.
Dim intSearch As Integer
If blnBackSpace = True Then ' Backspace is handled by KeyDown, but Handled property
e.Handled = True ' has to be set here
blnBackSpace = False
Return
End If
Dim strSaveSearch As String = strSearch ' Save in case of error
' Add newly typed character to list. If it's a special character, we need to surround with brackets
' or it'll be treated as a wildcard or some other special meaning.
strSearch &= CStr(IIf(InStr("~()#\/=><+-*%&|^'""[]", e.KeyChar) > 0, "[" & e.KeyChar & "]", e.KeyChar))
intSearch = FindMatch() ' Look for a match
If intSearch = -1 Then ' No match
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep) ' Play bell
strSearch = strSaveSearch ' Revert to prior search string
Else
dgv.CurrentCell = dgv.Item(0, intSearch) ' *** This assumes fldKey is the first column of the row
lblSearch.Visible = True
lblSearchText.Visible = True
lblSearchText.Text &= e.KeyChar
End If
End Sub
Private Sub dgv_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv.MouseClick
' If user clicks on a row, cancel the search process.
ClearSearch()
End Sub
Private Sub ClearSearch()
lblSearch.Visible = False
lblSearchText.Visible = False
lblSearchText.Text = ""
strSearch = ""
End Sub
Private Function FindMatch() As Integer
Dim drFind() As ds.dtRow
drFind = CType(dsA.dt.Select("fldKey LIKE '" & strSearch & "*'", "fldKey ASC"), ds.dtRow())
If drFind.Count = 0 Then
Return -1
Else
Return dv.Find(drFind(0).fldKey)
End If
End Function
End Class
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)