form.show set focus to underlying Form

user2373289 picture user2373289 · May 11, 2013 · Viewed 11.3k times · Source

D5pro.

I have a sub-form of the main app, Form1 with a Memo1 the the user can add text to.

I have a FormHelp that is only for viewing (contains MemoHelp.ReadOnly:=True).

In Form1 the FormHelp can be displayed by a button click on Form1, or automatically from a check box setting in the Setup.

Form1.OnShow...
if FormSetup.cbHelp.Checked then
  FormHelp.Show;

OR

Form1.BtnHelpClick(...
  FormHelp.Show;

I want to set the focus back to the Memo1 on Form1 after showing the Help window.

I have tried Memo1.SetFocus but that does not work.

I tried this Using WM_SETFOCUS and WM_KILLFOCUS and that did not work

I get an "Can't focus.." error with this How to force a focus on a component before the Form is shown

Can someone please show me how to put focus back to the Memo1

Thank you.

Answer

David Heffernan picture David Heffernan · May 11, 2013

You cannot call SetFocus on a control if its form does not have focus. And after the help form has shown, the memo's form no longer has focus. Instead, the help form has the focus.

So set the focus back to the form:

Form1.ActiveControl := Form1.Memo1;
Form1.SetFocus;