
Why doesn't SetFocus work fast enough?
Quote:
> If the focus is set on the appropriate scrollbar, these actions occur
>automatically. So, in what I thought was a great strike of genius, I set
>the form's KeyPreview to True, and in the KeyDown routine of the form,
>I coded a SetFocus on either the Vertical or Horizontal Scroll, based on
>the value of KeyCode, following that with a DoEvents.
> I can see the focus change as soon as I press down one of the keys I want
>to act on (the blinking square on the scrollbar confirms this), *but* if
>the scrollbar getting the focus is not the one that had it before, the key
>is sent to the scrollbar that had the focus before the key was pressed (in
>other words, it works *except* for the first keypress after I switch the
>focus from one to the other scrollbar).
I see the problem and the problem is a slight misunderstanding of yours how
SetFocus works.
What happens is this:
- You press a key
- Because of 'Preview' Form1_KeyDown is called
- In this sub you change the focus.
- Now the KeyDown event continues to the control which _orignally_ had the
focus, because SetFocus only changes the focus but not the internal
event queue which still points the the orignal control.
What you have to do is create a new KeyDown Event which you send to the
right scroll bar:
Sub Form1_KeyDown (KeyCode%, Shift%)
Select Case KeyCode%
Case KEY_PAGEUP, KEY_PAGEDOWN, KEY_DOWN, KEY_UP
VScroll.SetFocus 'You don't have to change the focus doing t this
'way, but if you like to...
VScroll_KeyDown KeyCode%, Shift%
Case KEY_LEFT, KEY_RIGHT
' Same for HScroll
End Select
KeyCode = 0 'Don't forget this or you might get double moves
End Sub
cu Robin