On KeyPress - How to detect keypress

I want to have my search textbox search when I hit the Enter key.
My code doesn't seem to work because it underlines "KeyCode" saying its not declared.

What do I need to do to make it work?

    Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
        If KeyCode = Keys.Enter Then
            Me.Text = "Hello"
        End If
    End Sub
LVL 1
Matrix1000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eozz_2000Commented:
Try with Key_Down Event
0
Matrix1000Author Commented:
Same thing....do I have to "Import" or "Inheret" aything...I've already got...

Imports System.Data.OleDb ' this is for oledbconnection stuff
Inherits System.Windows.Forms.Form
0
KarcOriginCommented:
Try this:

    Private Sub txtCommand_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCommand.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtCommand.Text = "Hello"
        End If
    End Sub
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

KarcOriginCommented:
No you do not need to Import or Inherit anything extra. I should work!
Cheers!
0
ptakjaCommented:
Also on your form, make sure the KeyPreview property is set to TRUE, otherwise the form won't raise these events.
0
arif_eqbalCommented:

Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
        If KeyCode = Keys.Enter Then
            Me.Text = "Hello"
        End If
    End Sub

My code doesn't seem to work because it underlines "KeyCode" saying its not declared

--->
When you write KeyCode as it is, it assumes it to be a variable and says it is not declared
the actual KeyChar is a member of the argument System.Windows.Forms.KeyPressEventArgs So use

e.KeyChar   (not KeyCode)

However you still would get an Error, that's because KeyChar returns a Char and Keys.Enter is ASCII value of Enter,  so your code should finally be

  If Asc(e.KeyChar) = Keys.Enter Then
         Me.Text = "Hello"
  End If

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
neuwkCommented:
Use the KeyUp Event on txtSearch instead of using KeyPress
0
planoczCommented:
sample code

   Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
       Dim KeyAscii As Short = Asc(e.KeyChar)
        Select Case KeyAscii
            Case System.Windows.Forms.Keys.Back   'same as 8
                KeyAscii = 0
            Case Keys.Enter, Keys.Tab             'same as 13
                'DO SOMETHING to SEARCH
                SendKeys.Send("{TAB}")
            Case 32 To 47
                e.Handled = True
            Case 48 To 57              'Numbers
            Case Else
                e.Handled = True
        End Select
    End Sub
0
ahmadfuwadCommented:
'Use following event it will work fine

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Enter Then
            MsgBox("Enter pressed")
        End If
    End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.