1
votes

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:

enter image description here

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:

enter image description here


2) After clicking on "Button1", this is what I can see:

enter image description here

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).

enter image description here

Form's caption became grayed.


4) I've reactivated the form by clicking on its caption:

enter image description here

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.

1
Why are you calling this function at all? I though we agreed that you aren't meant to do so.David Heffernan

1 Answers

5
votes

Despite what the documentation says, TForm.SetFocusedControl() does not actually set input focus onto Edit2 (the Win32 SetFocus() function is not called). Button1 receives the input focus when clicked on, and remains in focus after SetFocusControl() is called. That is why Edit2 does not render as focused. If you want to move input focus to Edit2, call Edit2.SetFocus() instead of Self.SetFocusedControl(Edit2).

However, calling SetFocusedControl() does change the VCL's internal state. It sets the Form's ActiveControl and FocusedControl properties, and it sets the global Screen object's ActiveControl, ActiveCustomForm, ActiveForm, and FocusedForm propeties and reorders the entries of its CustomForms and Forms properties.

When the Form is deactivated and then reactivated, the VCL sets input focus to the last known "focused" control, which is now Edit2 instead of Button1.