April 10, 2013

How to give focus to a FoxPro top-level form at start-up

By Mike Lewis

This is a question I often see in Visual FoxPro forums. An application needs to show a top-level form (typically a log-in screen) at start-up. But when you launch the form, it
appears behind other windows on the desktop. Even if it is at the front, it's not necessarily the active form. So how do you give the form focus programmatically?

None of the obvious techniques seem to work. These include toggling the form's AlwaysOnTop property, and calling the SetFocus method for one of its controls. However, the following code will always do the trick:

CLEAR DLLS "SetForegroundWindow"
All you have to do is place that code in the form's Init method, and the problem's solved. (The code works in VFP 7.0 and above.)

1 comment:

  1. Mike, you say the code only works in VFP 7.0 and above. I suspect that's because of the CLEAR DLLS in the last line. That form of the command was not available in earlier versions. Your suggestion will in fact work in VFP 6.0 and earlier if you simply remove the CLEAR DLLS. As far as I know, that will do no harm.