1. Home
  2. Computing & Technology
  3. Delphi Programming

Auto TabOrder
Programmatically Fix the TabOrder property in your Delphi applications

By , About.com Guide

Tip submitted by Jens Borrisholt

The TabOrder property exposed by all TWinControl descendant controls (such as TButton, TEdit, TComboBox, etc.) indicates the position of the control in its parent's tab order.

Tab order is the order in which child controls are visited when the user presses the Tab key.

Initially, the tab order is always the order in which the controls were added to the form.

Auto Tab Order - Programmatically

How many times have you "inserted" a visual component (control) between two control - and have forgotten to fix the tab order of the controls?

The SetChildTaborders function will programmatically fix the TabOrder property to all child controls for a given Parent.

Procedure SetChildTaborders(const Parent: TWinControl) ;

  procedure FixTabOrder(const Parent: TWinControl) ;
  var
    ctl, L: Integer;
    List: TList;
  begin
    List := TList.Create;
    try
      for ctl := 0 to Parent.ControlCount - 1 do
      begin
        if Parent.Controls[ctl] is TWinControl then
        begin
          if List.Count = 0 then
       L := 0
          else
          begin
            with Parent.Controls[ctl] do
              for L := 0 to List.Count - 1 do
                if (Top < TControl(List[L]).Top) or ((Top = TControl(List[L]).Top) and (Left < TControl(List[L]).Left)) then Break;
          end;

          List.Insert(L, Parent.Controls[ctl]) ;
          FixTabOrder(TWinControl(Parent.Controls[ctl])) ;
          end;
        end;

      for ctl := 0 to List.Count - 1 do
        TWinControl(List[ctl]).TabOrder := ctl;
    finally
      List.Free;
    end;
  end;
begin
  FixTabOrder(Parent) ;
end;
Note: TabOrder is meaningful only if the TabStop property is true.

Delphi tips navigator:
» Prevent a Delphi Form from Being Moved Off Screen « Get Subdirectories of a specified Directory from Delphi

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2008 Tips
  7. Auto TabOrder - Programmatically Fix the TabOrder property in your Delphi applications

©2009 About.com, a part of The New York Times Company.

All rights reserved.