FireMonkey forgot to add a changed event when changing TTabControl tabs/indexes

First, I love Delphi (20 years use) and FireMonkey (5 years use – Android).  My thoughts are criticisms, not a rejection.

Leave it to FireMonkey for making an experienced Delphi developer’s day frustrating.

When you change tabs on a TTabControl, chances are you want to place focus on a component on the current tab.  Well, there is no “OnChanged event” to accomplish this.  Luckily I found this code on a Russian website*.

    procedure DelayedSetFocus(control: TControl);
    begin
        TThread.CreateAnonymousThread(
        procedure
        begin
            TThread.Synchronize(nil,
                procedure
                begin
                    control.SetFocus;
                end);
        end).Start;
    end;

What this code does is delays setting focus on the “control” until the TTabControl tab changes to the new tab.

For example, you have a TTabControl with two (2) TTabItems named tiInfo and tiSysSet. You add code to the tab item you want to set focus on a control.

Code the following to the tab item click event:

    procedure TfrmHelp.tiSysSetClick(Sender: TObject);
    begin
        DelayedSetFocus(cmbCartPrint);
    end;

If coding for an iOS/Android, do not forget to add the tab item tap event.

    procedure TfrmHelp.tiSysSetTap(Sender: TObject);
    begin
        DelayedSetFocus(cmbCartPrint);
    end;

Simple and it works.

* http://qaru.site/questions/5727723/delphi-fmx-set-focus-to-a-particular-control-so-the-cursor-appears