[FMX]修正 FMX 组件嵌套时 Tab 顺序不正常的问题

问题发生在 FMX.Forms.AdvanceTabFocus,检测 Tab 顺序时,没有做深入测试,造成无法正确的处理。

下面的代码用于解决这个一问题,在窗体中增加:

protected 
   procedure IsDialogKey(const Key: Word; const KeyChar: WideChar; const Shift: TShiftState;
       var IsDialog: Boolean);override;

在对应的实现中,增加如下代码:

procedure TForm1.IsDialogKey(const Key: Word; const KeyChar: WideChar; const Shift: TShiftState;var IsDialog: Boolean);override;
//swish:2023.4.19
var
  APrior: IControl;
  ANext: IControl absolute APrior;
  function DoAdvance(AParent: TControl): Boolean;
  begin
    Result := False;
    if AParent.Enabled and AParent.Visible and (AParent.ControlsCount > 0) then
    begin
      if AParent.TabStop and AParent.CanFocus then
      begin
        if MoveForward then
        begin
          if APrior = Focused then
          begin
            AParent.SetFocus;
            Exit(True);
          end
          else
            APrior := AParent;
        end
        else
        begin
          if ANext = Focused then
          begin
            AParent.SetFocus;
            Exit(True);
          end
          else
            ANext := AParent;
        end;
      end;
      if MoveForward then
      begin
        for var I := 0 to AParent.ControlsCount - 1 do
        begin
          Result := DoAdvance(AParent.Controls[I]);
          if Result then
            break;
        end;
      end
      else
      begin
        for var I := AParent.ControlsCount - 1 downto 0 do
        begin
          Result := DoAdvance(AParent.Controls[I]);
          if Result then
            break;
        end;
      end
    end;
  end;

begin
  if Key=vkTab then
  begin
    for var I := 0 to ChildrenCount - 1 do
    begin
      var
      AChild := Children[I];
      if AChild is TControl then
      begin
        if DoAdvance(TControl(AChild)) then
          Exit;
      end;
    end;
  end;
  inherited;
end;

注意:上面的代码为免费版本代码,付费版本代码支持自动注入和焦点循环,付费金额:99元 付费方式

分享到: