form.show set focus to underlying Form

11,736

Solution 1

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;

Solution 2

FormHelp gains the input focus when it is shown. Try calling BringToFront() on Form1 before then calling Memo1.SetFocus().

Share:
11,736
user2373289
Author by

user2373289

Updated on June 04, 2022

Comments

  • user2373289
    user2373289 almost 2 years

    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.