How to make textbox responds to enter key?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 215756 Views - Last Post: 24 April 2012 - 08:43 AM Rate Topic: -----

#1 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

How to make textbox responds to enter key?

Posted 01 October 2008 - 08:42 PM

My Application contains a button and two text boxes.

1st textbox is for user input known as tbHost.Text

2nd textbox is for displaying results after button is clicked, this is set to readonly.

The button is a start button that will call a new process every time it is clicked.

I intend to let user to have a choice to either click on button to display result or press enter key to display result.

When user finished writing the host in the tbHost.Text, user only needs to press enter to display the results.

I could not seem to find an event in textbox that will response to a keyboard enter key...

any insights for me to look into?

Is This A Good Question/Topic? 0
  • +

Replies To: How to make textbox responds to enter key?

#2 Damage  Icon User is online

  • Lord of Schwing
  • member icon

Reputation: 56
  • View blog
  • Posts: 1,203
  • Joined: 05-June 08

Re: How to make textbox responds to enter key?

Posted 01 October 2008 - 09:46 PM

I can't remember what time frame you should look at exactly but i know this issue has been dealt with before here, use the search function and you'll get your answer :P

Have you tried setting the AcceptButton(under misc on the form) value on your form to the button you want clicked?

This post has been edited by Damage: 01 October 2008 - 09:58 PM

Was This Post Helpful? 2
  • +
  • -

#3 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to make textbox responds to enter key?

Posted 01 October 2008 - 11:51 PM

I managed to figure this out by myself.

For the benefit for other people who wonders how this is done, the codes are below.
declare this within your form class.
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short



or

Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort




Go to your user input text box for my case is tbHost.Text, write this code into the event tbHost_KeyDown:
if GetAsyncKeyState(key number) then
'do your job here
EndIf



For my case I only concern about return key which is number 13.

Upon keydown event if enter key is detected down, it will perform my task, the same task if user is to click start button
Was This Post Helpful? 2
  • +
  • -

#18 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: How to make textbox responds to enter key?

Posted 02 October 2008 - 04:34 PM

That will work, but that's way overcomplicated for something as simple as this. A slightly better way of doing it is to create a keydown event for your textbox (like you already did) and put this code in it
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                'Put whatever you want here.
            End If


Was This Post Helpful? 2

#19 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to make textbox responds to enter key?

Posted 02 October 2008 - 04:56 PM

I completely agree with jacobjordan, using Pinvoke for something this simple is way overkill
Was This Post Helpful? 0
  • +
  • -

#20 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to make textbox responds to enter key?

Posted 02 October 2008 - 07:46 PM

Thanks JacobJordan for giving me a better way of doing this :D :^: :^: :^:

It is much simpler this way :D

Declaring "user32.dll" lib is way too difficult to remember hahaha.... anyway I had a sense of achievement of able to find out overkill method of doing something simple.. at least I tried to look for solution for myself first haha...

Anyway how did you make the vbcodes with color? by using [vbcode]...[/vbcode]? I felt this way of showing my code is clearer...

This post has been edited by rakyomin: 02 October 2008 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#21 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: How to make textbox responds to enter key?

Posted 02 October 2008 - 08:33 PM

I used [code=vb][/code]
Here are the other ones i know:

[code=csharp][/code]
[code=cpp][/code]
[code=java][/code]
[code=php][/code]

There are many more, but i don't know them.
Was This Post Helpful? 1
  • +
  • -

#22 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to make textbox responds to enter key?

Posted 02 October 2008 - 08:38 PM

thank you for telling me JacobJordan :D :^: :^: :^:

Now i can include easier to read code in the forum :D
Was This Post Helpful? 0
  • +
  • -

#23 w_sampath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 08

Re: How to make textbox responds to enter key?

Posted 03 August 2009 - 11:09 AM

Thanks, I used it on my project.
Was This Post Helpful? 0
  • +
  • -

#24 vlogger3000  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 28-March 10

Re: How to make textbox responds to enter key?

Posted 28 March 2010 - 06:17 PM

View Postjacobjordan, on 02 October 2008 - 03:34 PM, said:

That will work, but that's way overcomplicated for something as simple as this. A slightly better way of doing it is to create a keydown event for your textbox (like you already did) and put this code in it
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                'Put whatever you want here.
            End If



when i use that i get this:
Error 1 'KeyCode' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 83 12 JweB 1.0
Error 2 'SuppressKeyPress' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 84 13 JweB 1.0

I'm trying to make a web browser and i want the URL bar to respond to enter.
I have a "Go" Button, with this line:
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)

but when i press enter after typing a URL i get a little error beep. what am i doing wrong?
Was This Post Helpful? 0
  • +
  • -

#25 HowDoIShotVB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-March 10

Re: How to make textbox responds to enter key?

Posted 28 March 2010 - 07:10 PM

View Postvlogger3000, on 28 March 2010 - 05:17 PM, said:

View Postjacobjordan, on 02 October 2008 - 03:34 PM, said:

That will work, but that's way overcomplicated for something as simple as this. A slightly better way of doing it is to create a keydown event for your textbox (like you already did) and put this code in it
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                'Put whatever you want here.
            End If



when i use that i get this:
Error 1 'KeyCode' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 83 12 JweB 1.0
Error 2 'SuppressKeyPress' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 84 13 JweB 1.0

I'm trying to make a web browser and i want the URL bar to respond to enter.
I have a "Go" Button, with this line:
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)

but when i press enter after typing a URL i get a little error beep. what am i doing wrong?

Resurrecting an ultra old thread.
Was This Post Helpful? 0
  • +
  • -

#26 Guest_Lindsay Bradford*


Reputation:

Re: How to make textbox responds to enter key?

Posted 08 September 2010 - 11:28 PM

View PostHowDoIShotVB, on 28 March 2010 - 06:10 PM, said:

View Postvlogger3000, on 28 March 2010 - 05:17 PM, said:

View Postjacobjordan, on 02 October 2008 - 03:34 PM, said:

That will work, but that's way overcomplicated for something as simple as this. A slightly better way of doing it is to create a keydown event for your textbox (like you already did) and put this code in it
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                'Put whatever you want here.
            End If



when i use that i get this:
Error 1 'KeyCode' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 83 12 JweB 1.0
Error 2 'SuppressKeyPress' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 84 13 JweB 1.0

I'm trying to make a web browser and i want the URL bar to respond to enter.
I have a "Go" Button, with this line:
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)

but when i press enter after typing a URL i get a little error beep. what am i doing wrong?

Resurrecting an ultra old thread.


The above code works if you follow jacobjordan's instructions to embed the code in a keydown handler. I've included code below that has a keydown handler using the concept:

    Private Overloads Sub URLTextBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles URLTextBox.KeyDown

        If e.KeyCode = Keys.Return Then
            getResource()
        End If
    End Sub



The errors you are seeing are the same ones I was seeing when attempting to do this with the TextChanged event (and its matching method signature using System.EventArgs for e). Once I moved to the KeyDown event, it worked straight out of the box.
Was This Post Helpful? 0

#27 emresutisna  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 80
  • Joined: 14-May 09

Re: How to make textbox responds to enter key?

Posted 09 September 2010 - 12:39 AM

View Postvlogger3000, on 28 March 2010 - 05:17 PM, said:

View Postjacobjordan, on 02 October 2008 - 03:34 PM, said:

That will work, but that's way overcomplicated for something as simple as this. A slightly better way of doing it is to create a keydown event for your textbox (like you already did) and put this code in it
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                'Put whatever you want here.
            End If



when i use that i get this:
Error 1 'KeyCode' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 83 12 JweB 1.0
Error 2 'SuppressKeyPress' is not a member of 'System.EventArgs'. C:\Users\Brooks\Documents\Visual Studio 2008\Projects\JweB 1.0\JweB 1.0\Form1.vb 84 13 JweB 1.0

I'm trying to make a web browser and i want the URL bar to respond to enter.
I have a "Go" Button, with this line:
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)

but when i press enter after typing a URL i get a little error beep. what am i doing wrong?

Try to change to be like this one :
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  If Asc(e.KeyChar) = 13 Then
    'Write whatever you want here
  End If
End Sub


Hope it helps
Was This Post Helpful? 0
  • +
  • -

#28 spatel_ce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 01-February 12

Re: How to make textbox responds to enter key?

Posted 01 February 2012 - 05:13 PM

View Postrakyomin, on 01 October 2008 - 08:42 PM, said:

My Application contains a button and two text boxes.

1st textbox is for user input known as tbHost.Text

2nd textbox is for displaying results after button is clicked, this is set to readonly.

The button is a start button that will call a new process every time it is clicked.

I intend to let user to have a choice to either click on button to display result or press enter key to display result.

When user finished writing the host in the tbHost.Text, user only needs to press enter to display the results.

I could not seem to find an event in textbox that will response to a keyboard enter key...

any insights for me to look into?


It has been such a long time, so this problem would have been solved. But here is a simplest error free method which I use in vb.net.

Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
                'Set Your Code Here
            End If
End Sub


Was This Post Helpful? 1
  • +
  • -

#29 vikxit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 23-April 12

Re: How to make textbox responds to enter key?

Posted 24 April 2012 - 08:39 AM

Quote

Have you tried setting the AcceptButton(under misc on the form) value on your form to the button you want clicked?


This is the answer. You must set the "AcceptButton" in the form properties.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2