Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focus on next component on (enter,up-down arrow key) press

Status
Not open for further replies.

silicon

Programmer
Aug 13, 2002
31
0
0
US
I want my control or focus to pass on next component by pressing enter key or up-down keys.I am using vb6.
I tried this code
sub form_keypress(index ...)
if keycode=13 then sendkeys("{TAB}")
if keycode=38 then sendkeys("{TAB}")
if keycode=40 then sendkeys("{TAB}")
end sub
The above code work fine for enter key only but the above event doesn't fire for up-down key press.
 
in each component lets say textboxes:

Private Sub txtSalesRep_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbkeyreturn or KeyCode = vbkeyup or KeyCode = vbkeydown then

txtWhatever.setfocus

End If
End Sub ----------------
Joe
 
Use the KeyDown event rather than KeyPress.

Keydown is raised when any key on the keyboard is pressed (as long as the form's keypreview is set to true) but the KeyPress event is only fired by a key combination that results in an ASCII value.
If you look in KeyDown, the return property is KeyCode. If you look in KeyPress, the return property is KeyAscii. Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Yes lovell811 is correct, the Keypress event doesn't fire when the arrow keys are pressed. The KeyDown event must be used instead. Keeping with your send keys idea here is my suggestion.

Private Sub TextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 37 To 40
SendKeys ("{TAB}")
End Select
End Sub
 
I'm not entirely sure but don't you need to use the keypress/keydown of the actual control in question to have any impact on the control behaviour. If so here is a couple of pieces of code you can use if you have created a control array.

private sub Text1_KeyPress(Index as integer,Keyascii as integer)
If keyascii = 13 and index < (Text1.Count - 1) then
text1(index+1).SetFocus
End If
End Sub

private sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
If keycode = 40 and index < (Text1.Count - 1) then
text1(index+1).setfocus
elseif keycode = 38 and index > 0 then
text1(index-1).setfocus
end if
end sub

It may not be the most advanced code around but it should work for what you are trying to do.

 
You can use the Form_Keydown event, which will fire on keydown anywhere on the form. That means that Silicon's original code doesn't have to be repeated in every control.

I stand by my previous comments on the different use of KeyPress and KeyDown Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Thanks everybody
I will repeat my qns i.e. in a form if there are n number of controls of different type say 1 is textbox,another is combobox and so on.The way we set the tabindex property of the control i.e first focus should be on textbox,next time it should be on combobox and so on.So those who had given reply for textbox event is not the solution for my problem.
And yes it should work with enter key and also up and down arrow key

 
Did you try the Form_Keydown event?
I tried your code in the Keydown event and it works for all controls except those that process the arrow keys internally, as their own keydown gets the event instead Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Thanks johnwm

Even I to tried in form_keydown event with keypreview property = true But only on enter key the focus goes to next control but on both up and down arrow the event keydown doesn't fire.
One more thing the num lock LED blinks every time when i hit enter key.
Is there any other method for up and down navigation with up and down arrow key.
Thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top