Why doesn't SetFocus work fast enough? 
Author Message
 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



Sat, 14 Mar 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. RichTextBox1.SetFocus - Why doesn't this work?

2. Why oh why doesn't this work?

3. .SetFocus doesn't always work

4. .SetFocus doesn't always work

5. Why LoadPicture() works on local pathes and doesn't work on the URLS

6. Code doesn't work, why?

7. Why doesn't this MAPISendMail work?

8. Why doesn't this button work?

9. SQL in VB doesn't work - why?

10. Why doesn't this work?

11. Why doesn't this work???

12. Why doesn't this work?!!

 

 
Powered by phpBB® Forum Software