If you're using ActiveForms, then you need to know about an ugly little secret. If you use MyDelphiForm.ActiveControl := Edit1, then you will get an exception stating 'Cannot focus a disabled or invisible window'. The reason for this is due to the implementation of TCustomForm.SetActiveControl. This setter method is called when you try to set ActiveControl on a form. The first thing it does is this:
if not ((Control = nil) or (Control <> Self) and (GetParentForm(Control) = Self) and ((csLoading in ComponentState) or Control.CanFocus)) then raise EInvalidOperation.Create(SCannotFocus);
The workaround is to use code like this to set the ActiveControl:
var ParentForm: TCustomForm; begin ParentForm := GetParentForm(Self); ParentForm.ActiveControl := Edit1;
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.