There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot browser bsod computer cpu crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware missing monitor network networking outlook outlook 2003 outlook 2007 outlook express password popups problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Detecting when "enter" is clicked in VB6


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
cj_white's Avatar
Senior Member with 335 posts.
 
Join Date: Oct 2001
09-May-2005, 07:31 PM #1
Detecting when "enter" is clicked in VB6
I am working on my final for my programming class. I have a login screen and I want to be able to hit enter to accept the user and pass instead of having to hit the command button. Also where would I have to put the code? Would it be for the text box or for the form? Thanks in advance
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
09-May-2005, 10:29 PM #2
Here is some food for thought, courtesy of MSDN Library, good luck.
The KeyPress Event

The KeyPress event occurs when any key that corresponds to an ASCII character is pressed. The ASCII character set represents not only the letters, digits, and punctuation on a standard keyboard but also most of the control keys. The KeyPress event only recognizes the ENTER, TAB, and BACKSPACE keys, however. The other function, editing, and navigation keys can be detected by the KeyDown and KeyUp events.

Use the KeyPress event whenever you want to process the standard ASCII characters. For example, if you want to force all the characters in a text box to be uppercase, you can use this event to change the case of the keys as they are typed:

Private Sub Text1_KeyPress (KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

The keyascii argument returns an integer value corresponding to an ASCII character code. The procedure above uses Chr to convert the ASCII character code into the corresponding character, UCase to make the character uppercase, and Asc to turn the result back into a character code.

Using the same ASCII character codes, you can test whether a key recognized by the KeyPress event is pressed. For instance, the following event procedure uses KeyPress to detect if the user is pressing the BACKSPACE key:

Private Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 8 Then MsgBox "You pressed the _
BACKSPACE key."
End Sub

You can also use the Visual Basic key-code constants in place of the character codes. The BACKSPACE key in the example above has an ASCII value of 8. The constant value for the BACKSPACE key is vbKeyBack.

For More Information For a complete list of character codes, see "Character Set (0–127)" and "Character Set (128–255)" in the Language Reference. A complete list of key code constants with corresponding ASCII values is available in "Key Code Constants" or by using the Object Browser and searching for KeyCodeConstants.

You can also use the KeyPress event to alter the default behavior of certain keys. For example, pressing ENTER when there is no Default button on the form causes a beep. You can avoid this beep by intercepting the ENTER key (character code 13) in the KeyPress event.

Private Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub

Just remember, you have to account for every situation in which a user might press Enter, not just the obvious one. Start by coding one, and when that works, add another trap.

Have fun.

MBN
__________________
I'll burn that bridge when I get to it!
GUSMAN's Avatar
Senior Member with 721 posts.
 
Join Date: Dec 2001
Location: WALES
11-May-2005, 03:44 AM #3
Hi
Im not an expert at VB, and not sure if this is what you want. but my tutor just advised me on a similar problem. I have a screen where you enter an account number, cash amount then press enter. then when you finish, on the same form you have a user, pass, and enter.
I was doing the same thing clicking the command button with the mouse, so he advised me to, do this.
when I open the form the account number label has focus (cursor is in the box) In the design mode (I think you call it that) I go to the properties of the account number and there's a "Tab Index" lets say the Tab is 10. I then set the Amount label to 12, and the enter button to 14. Then the username to 16, pass to 18 and "enter Password" button to 20.
So its like this. I start the form , enter the account number, "tab" to the amount label and enter the amount, "tab" to the enter button (or command button) and I can press the enter button on my keybard to go to the next form. When I go back to form 1, the cursor is back in the account number label. I press "tab" 4 times until it is in the username label, enter username "tab" to put in password,enter password "tab" to highlight the enter button and press enter on the keyboard to go to the relevant form.
No mouse needed at all.

Hope this helps

Cheers

Gus

PS just play around with it.
MyName's Avatar
Senior Member with 320 posts.
 
Join Date: Oct 2001
Location: Ohio
Experience: Advanced
11-May-2005, 07:16 AM #4
Probably not what you really wanted, but you could just make a command button named Enter and then on your Enter button, in the properties box change the "Default" value to "True"
GUSMAN's Avatar
Senior Member with 721 posts.
 
Join Date: Dec 2001
Location: WALES
11-May-2005, 12:19 PM #5
I have 2 enter buttons on my form so this way suits me better
cj_white's Avatar
Senior Member with 335 posts.
 
Join Date: Oct 2001
11-May-2005, 05:02 PM #6
This is what I ended up doing.

'Detect to see if enter button is hit after the password field has become active
Private Sub txtPass_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
'If the enter button has been hit call Check_credentials function to verify login
Call Check_Credentials(txtUser.Text, txtPass.Text)
End If
End Sub

It is almost exactly what MustBNuts posted from the MSDN site (I never thought to look there). I had trouble with it at first but it now works. Thanks for all the posts guys
Reply


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

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 On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:23 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.