First of all, my goal isn't that to set the focus on a control but I'm trying to understand why a form has a different appearance after deactivating and reactivating it.
Here is TCustomForm.SetFocusedControl function's description:
Sets focus to a control on the form.
Use SetFocusedControl to give a control on the form input focus. SetFocusedControl returns false if the Control specified by the Control parameter was already in the process of receiving focus, true otherwise.
Note: A return value of true does not indicate the control has successfully received input focus. If the Control can't have focus (for example, if it is not visible), SetFocusedControl will still return true, indicating that an attempt was made.
I've created a simple test application in order to reproduce the observed behavior:
procedure TForm1.Button1Click(Sender: TObject);
var
Res : Boolean;
begin
Res := Self.SetFocusedControl(Edit2);
if(Self.ActiveControl <> nil) then
begin
Memo1.Text :=
'ActiveControl is ' + Self.ActiveControl.Name + sLineBreak +
'SetFocusedControl result is ' + BoolToStr(Res, True);
end;
end;
Here are steps I followed to reproduce that behavior:
1) At start, the form appears as follows:
2) After clicking on "Button1", this is what I can see:
Memo1.Text reports that Edit2 is the active control but it hasn't the tipical focused appearance (selection and cursor).
Form's caption isn't grayed and clicking on it doesn't cause any change.
3) I've clicked outside the form (on the windows taskbar).
Form's caption became grayed.
4) I've reactivated the form by clicking on its caption:
Edit2 now looks like it has focus.
Could someone explain the differences between the form at point 2 and at point 4? In both cases, Edit2 was the active control and I can't understand that appearance difference.
Further informations:
Tested on Delphi 2007, Windows 10 Pro.