?
Solved

On KeyPress  - How to detect keypress

Posted on 2004-11-16
9
Medium Priority
?
719 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Matrix1000
9 Comments
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12599760
Try with Key_Down Event
0
 
LVL 1

Author Comment

by:Matrix1000
ID: 12599808
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
 
LVL 5

Assisted Solution

by:KarcOrigin
KarcOrigin earned 200 total points
ID: 12599809
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:KarcOrigin
ID: 12599825
No you do not need to Import or Inherit anything extra. I should work!
Cheers!
0
 
LVL 14

Assisted Solution

by:ptakja
ptakja earned 200 total points
ID: 12600273
Also on your form, make sure the KeyPreview property is set to TRUE, otherwise the form won't raise these events.
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 1200 total points
ID: 12600924

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
 
LVL 1

Expert Comment

by:neuwk
ID: 12603397
Use the KeyUp Event on txtSearch instead of using KeyPress
0
 
LVL 27

Assisted Solution

by:planocz
planocz earned 200 total points
ID: 12604400
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
 
LVL 2

Assisted Solution

by:ahmadfuwad
ahmadfuwad earned 200 total points
ID: 12613767
'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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question