Yup, it's time to talk about ActiveForms again.
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);
Note the part where it is trying to assert that the root ParentForm for the Control you are trying to set focus to is the same as the current form. This works fine in regular windows applications, but unfortunately in ActiveForms, the root ParentForm is the actual ActiveForm. Most people use that form as nothing more than a container to then host their forms inside. I wrote about this technique in a previous Delphi Informant, and it has been posted plenty of places on the Internet as well (e.g.
Conrad Hermann had the first mention of this that I can remember).
The workaround is to use code like this to set the ActiveControl:
var
ParentForm: TCustomForm;
begin
ParentForm := GetParentForm(Self);
ParentForm.ActiveControl := Edit1;