vb6 how capture enter key press?

I have a VB 6 application in which I have several text boxes on a form.  When the user types in a number in the first text box and presses the Enter key, I want the application to set focus to the second text box, etc.

I have done this in previous application to speed up data entry, but now when I try it, it does not seem to work.  Below is the code I am using.

Private Sub txtContract_Labor_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then       'User pressed the Enter key
        txtContract_Materials.SetFocus
    End If
End Sub

This code below does not even fire off when the enter key is pressed:

Private Sub txtContract_Labor_KeyDown(KeyCode As Integer, Shift As Integer)
    MsgBox KeyCode
End Sub

Any ideas why it does not work?  Thanks.
LVL 7
EYoungAsked:
Who is Participating?
 
Mark_FreeSoftwareCommented:

no it is the default property of the button


that chatches the enter key
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It works fine for me:

Private Sub txtContract_Labor_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        KeyAscii = 0
        SendKeys "{TAB}"
    End If
End Sub
0
 
superg65Commented:
You might need to set the form property KeyPreview on your form to True to catch these key presses.

:)
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes, if you're using the Form_KeyPress() event...but it looks like he is trapping the KeyPress() event for a TextBox called "txtContract_Labor".

The form KeyPress() event (which would need KeyPreview set to True) would be:

    Private Sub Form_KeyPress(KeyAscii As Integer)

    End Sub
0
 
superg65Commented:
Haha Idle Mind ever lurking.

You are quite right -  KeyPreview refers to form level action - my mistake. The code supplied worked for me also - sorry I can't see anything worng with it.

:(
0
 
Mark_FreeSoftwareCommented:

make sure that is the actual name of your textbox!

doubleclick on it when in designerview to check this

if it is the right one you should end with the cursor in this sub
0
 
EYoungAuthor Commented:
Thanks everyone for the input.  I entered the KeyPress code above and it does get triggered when all keys are pressed "except" the Enter key.  (The shift, cntr, and alt keys also do not trigger the event, but I only care about knowing when the Enter key is pressed.)

I don't get this as I have used this logic before and it has worked perfectly.  The text box is on a frame which is on the form but that should not matter as the text box does have focus.  I also did confirm the name of the text box by double clicking in design view mode and it is the correct name.

I have set a breakpoint at the KeyPress line and it is not triggered when the Enter key is pressed.  It is only triggered when the numeric and alpha keys along with the + and - signs are pressed.

Any ideas?  Thanks.
0
 
Mark_FreeSoftwareCommented:

make sure the enter key is not broken!

if that isn't the problem, try posting the settings of the textbox that is giving the problem

also post if it is the intrinsic one or one from forms 2.0 or a rtf etc.....


also Enter is not an english word (as far as i know), i think you mean Return

0
 
EYoungAuthor Commented:
1.  The Enter key is not broken.  I tested it in another applicaton I wrote and it works the way it is suppose to.

2.  I use the term Enter and that is what is printed on the key.

3.  Good question about the intrinsic one or one from Forms 2.0 or rtf etc.  It is the intrinsic one.  In fact, I copied a new textbox to the form and tried it and it does not work either.  Very strange.

4.  Below are the settings of the textbox:  

txtContract_Labor TextBox

Appearance Flat
BorderStyle 1-Fixed Single
HideSelection True

CausesValidation True
0
 
EYoungAuthor Commented:
4.  Below are the settings of the textbox:  

txtContract_Labor TextBox

Appearance Flat
BorderStyle 1-Fixed Single
HideSelection True

CausesValidation True
Enabled True
Locked False
MaxLength 20
MultiLine False
ScrollBars 0-None
TabIndex 41
TabStop True
Visible True

Name txtContract_Labor
Alignment 1-Right Justify
HelpContextID 0
Index
MouseIcon (None)
MousePointer 0-Default
PasswordChar
Tag
Text
ToolTipText
WhatThisHelpID 0


I did not list all of the properties.

Thanks for the help
0
 
Mark_FreeSoftwareCommented:

try putting your code in the keyUp event, and replace keyascii with KeyCode

does that work?


is there any subclassing in your app that is catching keys?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try a new project with just a textbox (no frames or anything else) and the code.  

Does the problem persist?
0
 
EYoungAuthor Commented:
I found part of the problem.  I have on the form some command buttons.  One of them (the "Add" button) I had set the Default property to True.  When I changed it to False, then when I press the Enter key on the txtContract_Labor control, the KeyPress event does work correctly.

Possibly contributing further to the problem is the fact that I have set up some automatic help text that triggers whenever the cursor moves over any control on the form.  I user each control's MouseMove event.  That might be causing the txtControl_Labor text box to loose control?  That does not seem to be happening but I can't figure out why setting the Default property on the "Add" command button from True to False allows the KeyPress event to work on the text box.

Thanks for the input.
0
 
EYoungAuthor Commented:
I don't get that.  Does setting the command button's default property from False to True mean that anytime the enter key is pressed anywhere on the form, the command button is triggered?  I guess that makes sense.

Thanks for all the help.  I am going to increase the points and split them.
0
 
Mark_FreeSoftwareCommented:
I don't get that.  Does setting the command button's default property from False to True mean that anytime the enter key is pressed anywhere on the form, the command button is triggered?

that's true
try setting a messagebox in the command_click of that particular commandbutton and is should fire


in fact this is the same with the cancel property, but then for the escape key
0
 
EYoungAuthor Commented:
Yes.  The msgbox did fired off when I pressed the Enter key while the text box had focus.  I guess that makes sense - kind of...

Thanks to both of you for the help.  I think everything is fine now.  Whew...  I am off and running now with the project.
0
 
Mark_FreeSoftwareCommented:

thanx for the points and happy coding!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.