Question:
Visual Basic: How can i get it to launch a sub when the user presses the enter key in a textbox??
Daniel H
2007-05-28 03:55:33 UTC
im trying to add a chat sort of thing to my app. any help would be great
Six answers:
Fox
2007-05-28 04:32:09 UTC
00 Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)

02     If KeyCode = KeyCodeConstants.vbKeyReturn Then

03         'TODO: Add your code here or call the required sub

04

05         'Example:

06         MsgBox "welcome in vb world!", vbOKOnly Or vbInformation

07     End If

08 End Sub



That will execute the associated sub or code when the KeyUp event got triggered; i.e. when the enter key gos up. To execute the action when the key is down, just change the KeyUp word to KeyDown, that will execute the code whenever the KeyDown event is triggered.



Thanks.





EDIT



Oh, all these answers, lol



Stick to "Hello There" answer, his answer is totally correct.



My answer is for vb 6, you know, you didn't tell which version you are talking abouat.



Thanks.
Chie
2007-05-28 04:04:47 UTC
EDIT #3:

Well you can select the KeyDown or KeyPress event in your control (the textbox) and insert your sub's name:



'' Visual Basic 8.0

Imports System

Imports Windows.Forms

Public Sub TextBox1_KeyDown(obj As Object, e As KeyPressEventArgs) Handles TextBox1.KeyDown

If e.Key = Keys.Enter Then Call MySub()

End Sub



That's it. The Call statement is optional.



EDIT #4:

About the Additional Details

To pick up the Enter key :

IF E.KEY = KEYS.ENTER

:) as above.



EDIT #5

theFOX is right, you're probably using VB 8.0 or 9.0



EDIT #6

Here's how to do the same thing in VB6:



'' Visual Basic 6

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then Call MySub

End Sub



Anyway, you got it working, right?
matzen
2016-10-06 08:27:15 UTC
you're utilising a thoroughly diverse adventure. Use the KeyDown adventure. right here, I edited for you :P inner maximum Sub textbox_KeyDown(ByVal sender As merchandise, ByVal e As device.domicile windows.varieties.KeyEventArgs) Handles textbox.KeyDown If e.KeyCode = Keys.enter Then If textbox.text textile = solutions(picturebox.Tag) Then picturebox.Tag = Rand(0, MAX) textbox.text textile = "" blend = blend + a million combobox.text textile = "blend: " & blend Dim strPath As String = "C:" & picturebox.Tag & ".png" picturebox.ImageLocation = strPath end If end If end Sub the place "Keys.enter" is, replace "enter" to what key you elect to apply to set off the KeyDown adventure. it is for VB2008, btw.
Vinoth
2007-05-28 04:03:44 UTC
select the keypress event of the text box and check the ascii value for the keypressed if the ascii value is 13 that the ascii value for enter you call ur subroutine thats all
Tejaswini
2007-05-28 04:06:17 UTC
put the sub in a separate form, say form2.



double click on your form.

in the form.load area, type

form2.hide



in the 'on click' statement for the button etc, write form2.show
hott_pakistani
2007-05-28 04:53:54 UTC
Simple :



e.g lets name it "text1"



-goto its events and change it to KEYPRESS



Now type the following script



{

if keyascii = 13 then

(whatever you wanna do)

(e.g text2.text = text1.text -- this will copy whatever you've written in text1 to text2)

end if

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...