Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > move from one textbox to another with the ENTER key


Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2004, 12:55 PM
mickymouse mickymouse is offline
Centurion
 
Join Date: Dec 2003
Posts: 171
Default move from one textbox to another with the ENTER key


how can i move from one textbox to the other with the Enter key please?
Reply With Quote
  #2  
Old 01-09-2004, 12:58 PM
coffee_machine's Avatar
coffee_machine coffee_machine is offline
Regular
 
Join Date: Jan 2004
Location: Switzerland
Posts: 52
Default

Quote:
Originally Posted by mickymouse
how can i move from one textbox to the other with the Enter key please?



Use the event KeyPress of the textbox and add the following code in this event :

If keyascii = vbKeyReturn then
txtText2.setfocus ' txtText2 is your second textbox
End if
Reply With Quote
  #3  
Old 01-09-2004, 01:02 PM
00100b's Avatar
00100b 00100b is offline
Martian In Disguise

Retired Moderator
* Guru *
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 9,566
Default

Along the same lines, to allow the TabOrder of the controls on the form to be taken into consideration, then use something like:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyReturn Then SendKeys "{TAB}" KeyAscii = 0 End If End Sub
Setting the KeyAscii to 0 after sending the Tab key will negate the Enter key press. Your basically swapping one key press for another.
__________________
The only stupid question is the one that goes un-asked.
Reply With Quote
  #4  
Old 01-09-2004, 01:02 PM
GamesRule GamesRule is offline
Junior Contributor
 
Join Date: Apr 2003
Location: Milton Keynes
Posts: 385
Default

Quote:
Originally Posted by coffee_machine
Quote:
Originally Posted by mickymouse
how can i move from one textbox to the other with the Enter key please?



Use the event KeyPress of the textbox and add the following code in this event :

If keyascii = vbKeyReturn then
txtText2.setfocus ' txtText2 is your second textbox
End if


Thought U had to do it in KeyCode = vbKeyReturn not KeyAscii or can you use both?
__________________
Ian
The Only Way To Be Totally Right, Is To Lie.
Reply With Quote
  #5  
Old 01-09-2004, 01:04 PM
GamesRule GamesRule is offline
Junior Contributor
 
Join Date: Apr 2003
Location: Milton Keynes
Posts: 385
Default

Quote:
Originally Posted by coffee_machine
Quote:
Originally Posted by mickymouse
how can i move from one textbox to the other with the Enter key please?



Use the event KeyPress of the textbox and add the following code in this event :

If keyascii = vbKeyReturn then
txtText2.setfocus ' txtText2 is your second textbox
End if

__________________
Ian
The Only Way To Be Totally Right, Is To Lie.
Reply With Quote
  #6  
Old 01-09-2004, 02:43 PM
mickymouse mickymouse is offline
Centurion
 
Join Date: Dec 2003
Posts: 171
Default

Quote:
Originally Posted by 00100b
Along the same lines, to allow the TabOrder of the controls on the form to be taken into consideration, then use something like:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyReturn Then SendKeys "{TAB}" KeyAscii = 0 End If End Sub
Setting the KeyAscii to 0 after sending the Tab key will negate the Enter key press. Your basically swapping one key press for another.



Am I getting you right? will it be like this? Sorry guys im a beginner?Thanks to you all.

Private Sub Text1_KeyPress(KeyAscii As Integer)

If keyascii = vbKeyReturn then
txtText2.setfocus ' txtText2 is your second textbox
End if

If KeyAscii = vbKeyReturn Then
SendKeys "{TAB}"
KeyAscii = 0
End If
End Sub
Reply With Quote
  #7  
Old 01-09-2004, 03:02 PM
samr samr is offline
Senior Contributor
 
Join Date: Jul 2003
Location: London, England
Posts: 993
Default

You have 2 different things which do similar things there. The first:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If keyascii = vbKeyReturn then txtText2.setfocus ' txtText2 is your second textbox End if End Sub
This sets the focus to a set object when you press the enter key, or:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyReturn Then SendKeys "{TAB}" KeyAscii = 0 End If End Sub
which will move the focus as if the tab key on the keyboard has been pressed, so will take the focus to the object with a tab index of one higher than the present object.

GamesRule:
KeyCode is for the keyup/down subs, KeyAscii is for the keypress sub. The constants for the keycode also work with keyascii, but will give the ascii value of the capital when there is two. KeyAscii does not hold codes for special keys - ie thoes that do not give letter to the program when pressed
Reply With Quote
  #8  
Old 01-10-2004, 01:35 PM
mickymouse mickymouse is offline
Centurion
 
Join Date: Dec 2003
Posts: 171
Default

Quote:
Originally Posted by samr
You have 2 different things which do similar things there. The first:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If keyascii = vbKeyReturn then txtText2.setfocus ' txtText2 is your second textbox End if End Sub
This sets the focus to a set object when you press the enter key, or:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyReturn Then SendKeys "{TAB}" KeyAscii = 0 End If End Sub
which will move the focus as if the tab key on the keyboard has been pressed, so will take the focus to the object with a tab index of one higher than the present object.

GamesRule:
KeyCode is for the keyup/down subs, KeyAscii is for the keypress sub. The constants for the keycode also work with keyascii, but will give the ascii value of the capital when there is two. KeyAscii does not hold codes for special keys - ie thoes that do not give letter to the program when pressed

Reply With Quote
  #9  
Old 01-10-2004, 02:07 PM
Jos de Bosduif's Avatar
Jos de Bosduif Jos de Bosduif is offline
Regular
 
Join Date: Jun 2003
Location: Belgium (Ternat)
Posts: 62
Default

(another solution? )
Are you familiar with control arrays?
If you put the textboxes in a control array the code could be like
Code:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = vbKeyReturn Then If Index < Text1.UBound Then Text1(Index + 1).SetFocus Else Text1(0).SetFocus End If End If End Sub
Reply With Quote
  #10  
Old 01-10-2004, 02:18 PM
mickymouse mickymouse is offline
Centurion
 
Join Date: Dec 2003
Posts: 171
Default

Quote:
Originally Posted by Jos de Bosduif
(another solution? )
Are you familiar with control arrays?
If you put the textboxes in a control array the code could be like
Code:
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = vbKeyReturn Then If Index < Text1.UBound Then Text1(Index + 1).SetFocus Else Text1(0).SetFocus End If End If End Sub



Well, mine are not in an array but I can use this code for the future cos it is another good way. ths for helping
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Enter after textbox entry ice_sculpture General 5 09-28-2003 06:27 PM
Error in updating database thro textbox (editable msflexgrid) SURESH MENON Database and Reporting 0 03-26-2003 10:57 PM
Textbox and Enter key Memily General 5 10-23-2002 02:15 PM
value in textbox cannot move next(HELP!) kinki General 2 06-26-2002 10:46 AM
Can't enter HEX numbers in value textbox? vbfourme General 8 12-10-2001 10:49 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->