Cannot Focus Disabled Control
Delphi Programming Language
Severity: MinorWhat Does This Error Mean?
This error occurs when code calls SetFocus on a control that is disabled, hidden, or whose parent window is not ready. Windows cannot give keyboard focus to a control that the user cannot interact with. The fix is to check the control's state before calling SetFocus, or to enable and show the control first.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- Calling SetFocus on a control with Enabled = False
- Calling SetFocus on a control with Visible = False
- Calling SetFocus before the parent form has been fully created and shown
- The control is on a hidden tab or panel that is not currently visible
- Calling SetFocus inside FormCreate before the form's window handle exists
How to Fix It
-
Replace any bare SetFocus calls with a guarded version: if MyControl.CanFocus then MyControl.SetFocus. The CanFocus method checks all the conditions Windows requires for focus.
CanFocus returns True only when the control and all its parents are visible, enabled, and have valid handles.
-
If the control is intentionally disabled and you need to focus it, enable it first: MyControl.Enabled := True; MyControl.SetFocus;
Always enable before focusing. You cannot focus a disabled control — this is a Windows rule, not just a Delphi rule.
-
If the error occurs in FormCreate, move the SetFocus call to FormShow or use Application.ProcessMessages followed by SetFocus to allow the form to finish creating.
Better yet, use PostMessage(Handle, WM_APP, 0, 0) in FormCreate and handle it in a message handler that calls SetFocus — this defers execution until the form is fully ready.
-
For controls on tab sheets, ensure the correct TTabSheet is active before focusing a control on it: PageControl.ActivePage := MyTabSheet; then call SetFocus on the control.
Controls on an inactive tab sheet are not visible to Windows and cannot receive focus.
-
Check for nested parent visibility. A control can be Enabled and Visible but still fail CanFocus if a parent panel, group box, or page control is hidden or disabled.
CanFocus walks up the parent chain — all ancestors must be enabled and visible for a control to be focusable.
When to Call a Professional
The error is usually minor and easy to fix by checking CanFocus before calling SetFocus. If it occurs during application startup, move the SetFocus call to FormShow or use PostMessage to defer it. For tab-based interfaces, ensure the correct tab page is selected before trying to focus a control on it.
Frequently Asked Questions
Why does SetFocus work when I click a button but fail when called from code?
When you click a button, Windows handles the focus change through the normal message queue, bypassing restrictions. When you call SetFocus from code, it goes through the Delphi VCL which performs validity checks and raises an exception if the control is not focusable. The solution is always to check CanFocus first.
Is 'Cannot focus a disabled or invisible window' an exception or a message?
It is raised as an EInvalidOperation exception in the Delphi VCL. If you do not have an exception handler for it, it will appear as an error dialog to the user. Catch EInvalidOperation or use CanFocus to prevent it from being raised at all.
Does this error affect FireMonkey (FMX) applications too?
FMX uses a different focus model from VCL. FMX controls have a SetFocus method but the conditions and error messages differ. The general principle is the same: a control must be enabled and visible to receive focus, but FMX handles it differently internally.