15 Replies - 151389 Views - Last Post: 24 April 2012 - 08:43 AM
#1
How to make textbox responds to enter key?
Posted 01 October 2008 - 08:42 PM
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?
Replies To: How to make textbox responds to enter key?
#2
Re: How to make textbox responds to enter key?
Posted 01 October 2008 - 09:46 PM
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
#3
Re: How to make textbox responds to enter key?
Posted 01 October 2008 - 11:51 PM
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
#18
Re: How to make textbox responds to enter key?
Posted 02 October 2008 - 04:34 PM
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
'Put whatever you want here.
End If
#19
Re: How to make textbox responds to enter key?
Posted 02 October 2008 - 04:56 PM
#20
Re: How to make textbox responds to enter key?
Posted 02 October 2008 - 07:46 PM
It is much simpler this way
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
#21
Re: How to make textbox responds to enter key?
Posted 02 October 2008 - 08:33 PM
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.
#22
Re: How to make textbox responds to enter key?
Posted 02 October 2008 - 08:38 PM
Now i can include easier to read code in the forum
#23
Re: How to make textbox responds to enter key?
Posted 03 August 2009 - 11:09 AM
#24
Re: How to make textbox responds to enter key?
Posted 28 March 2010 - 06:17 PM
jacobjordan, on 02 October 2008 - 03:34 PM, said:
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?
#25
Re: How to make textbox responds to enter key?
Posted 28 March 2010 - 07:10 PM
vlogger3000, on 28 March 2010 - 05:17 PM, said:
jacobjordan, on 02 October 2008 - 03:34 PM, said:
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.
#26 Guest_Lindsay Bradford*
Re: How to make textbox responds to enter key?
Posted 08 September 2010 - 11:28 PM
HowDoIShotVB, on 28 March 2010 - 06:10 PM, said:
vlogger3000, on 28 March 2010 - 05:17 PM, said:
jacobjordan, on 02 October 2008 - 03:34 PM, said:
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.
#27
Re: How to make textbox responds to enter key?
Posted 09 September 2010 - 12:39 AM
vlogger3000, on 28 March 2010 - 05:17 PM, said:
jacobjordan, on 02 October 2008 - 03:34 PM, said:
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
#28
Re: How to make textbox responds to enter key?
Posted 01 February 2012 - 05:13 PM
rakyomin, on 01 October 2008 - 08:42 PM, said:
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
#29
Re: How to make textbox responds to enter key?
Posted 24 April 2012 - 08:39 AM
Quote
This is the answer. You must set the "AcceptButton" in the form properties.
|
|

New Topic/Question
Reply




MultiQuote




|