Kiogara
Kiogara

Reputation: 657

Delphi - Why is this tab order not working properly?

I'm creating a few panels, with a TEdit inside, dynamically. I create them from bottom to top so I need to reverse the TabOrder and they're created inside a frame that appear in other form. However, when I try to reverse it I get the wrong order(0-4-1-3-2), if I don't, I get the creation order(4-3-2-1-0), as expected.

Here is the code for the main form:

type
  TForm1 = class(TForm)
    Frame: TFrame1;
    procedure FormCreate(Sender: TObject);
  end;
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Frame := TFrame1.Create(Self);
  Frame.Parent := Self;
end;

and here for the frame and panel:

type
  TFrame1 = class(TFrame)
  public
    constructor Create(aOwner: TComponent);
  end;

  TMyPanel = class(TPanel)
  public
    FEdit1: TEdit;
    constructor Create(aOwner: Tcomponent; str: string);
  end;

implementation

{$R *.dfm}

constructor TFrame1.Create(aOwner: TComponent);
var
 Panel: TMyPanel;
 I: integer;
begin
  inherited Create(aOwner);
  for I := 4 downto 0 do
  begin
    with TMyPanel.Create(Self, IntToStr(I)) do begin
      Align := alTop;
      Parent := Self;
      Top := 10 * I;
      TabOrder := I;
    end;
  end;
end;

constructor TMyPanel.Create(aOwner: Tcomponent; str: string);
begin
  inherited Create(aOwner);
  Caption := 'order ' + str;
  FEdit1 := TEdit.Create(Self);
  FEdit1.Align := alRight;
  FEdit1.Parent := Self;
  FEdit1.SetSubComponent(True);
end;

Upvotes: 2

Views: 1706

Answers (1)

Sertac Akyuz
Sertac Akyuz

Reputation: 54812

You can't set tab order to a number that's greater than the count of controls that can have their tab order set.

In the first iteration in the for loop you are creating the only control (the first panel) that's parented in the frame and then setting its tab order to 4. The tab order, actually is set to 0. You need to change your algorithm.

Upvotes: 4

Related Questions