Cannot focus a disabled or invisible window

Delphi Programming Language

Severity: Minor

What it means

The message: Cannot focus a disabled or invisible window.
Code called SetFocus on a control, or a form, that can't take the focus yet: it's hidden or disabled, or a control that contains it is.
Embarcadero: CanFocus returns true only when the control and its parents all have Visible and Enabled set to true.

Affected Models

  • Delphi programs at run time

How to Fix It

  1. SetFocus in a form's OnCreate? The form isn't visible yet. Stack Overflow's accepted answer: move that code to OnShow.

    Or set the form's ActiveControl to that control. It gets the focus when the form appears.

  2. Test first: if Edit1.CanFocus then Edit1.SetFocus;

  3. A control on a hidden tab sheet, or in a hidden or disabled panel? It can't take the focus until that container is shown and enabled. Show the tab first, then set the focus.