Foxseiz
Foxseiz

Reputation: 300

KeyPress is acting weird, but button click is doing fine VB.net

I'm using the code below to do a search in my database but my keypress is acting weird.

Private Sub txtSearch4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSearch4.KeyPress
   Call btnSearch4_Click(sender, e)
End Sub

Private Sub btnSearch4_Click(sender As Object, e As EventArgs) Handles btnSearch4.Click
    If SQL.HasConnection = True Then
        SQL.ExecQuery("SELECT Customer, Product, Part_Number FROM Product WHERE (" & cmbType4.Text & " LIKE '%" & txtSearch4.Text & "%') ORDER BY Part_Number ASC")

        If SQL.DBDS.Tables.Count > 0 Then
            dgvProd.DataSource = SQL.DBDS.Tables(0)
        End If
    End If
End Sub

Video: https://drive.google.com/open?id=0B7BUg5POgN7dZGVBTUpad1FWdDg As you can see in the video, when I press a key it doesn't fire up right away. I want it this way, when I press a single key, it will auto fire like how you press a button. Thanks guys.

Upvotes: 0

Views: 136

Answers (1)

Jean-François
Jean-François

Reputation: 382

Use the KeyDown event instead. This event is raised as soon as you press a key on the keyboard whereas KeyPress event is raised for character keys while the key is pressed and THEN released by the user.

Private Sub txtSearch4_KeyDown(sender As Object, e As KeyPressEventArgs) Handles txtSearch4.KeyDown
   doSearch()
End Sub

Private Sub btnSearch4_Click(sender As Object, e As EventArgs) Handles btnSearch4.Click
   doSearch()
End Sub

Private Sub doSearch()
    If SQL.HasConnection = True Then
        SQL.ExecQuery("SELECT Customer, Product, Part_Number FROM Product WHERE (" & cmbType4.Text & " LIKE '%" & txtSearch4.Text & "%') ORDER BY Part_Number ASC")

        If SQL.DBDS.Tables.Count > 0 Then
            dgvProd.DataSource = SQL.DBDS.Tables(0)
        End If
    End If
end sub

Upvotes: 1

Related Questions