Link to home
Start Free TrialLog in
Avatar of __alex
__alex

asked on

Tab order

By pressing the tab key I don'want to cycle through all the controls on my form. Say I've got to panels. If a control on the the right panel is focused I want to cycle through controls on the right panel, same for the left panel. Can anyone help? Thank you!
Avatar of calinutz
calinutz
Flag of Romania image

You mean like this?

object Form2: TForm2
  Left = 206
  Top = 284
  Width = 696
  Height = 480
  Caption = 'Form2'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 48
    Top = 64
    Width = 185
    Height = 229
    Caption = 'Panel1'
    TabOrder = 0
    object Edit1: TEdit
      Left = 40
      Top = 24
      Width = 121
      Height = 21
      TabOrder = 0
      Text = 'Edit1'
    end
    object Edit2: TEdit
      Left = 40
      Top = 60
      Width = 121
      Height = 21
      TabOrder = 1
      Text = 'Edit2'
    end
    object Button1: TButton
      Left = 100
      Top = 124
      Width = 75
      Height = 25
      Caption = 'Focus other'
      TabOrder = 2
      OnEnter = Button1Enter
    end
  end
  object Panel2: TPanel
    Left = 296
    Top = 68
    Width = 185
    Height = 233
    Caption = 'Panel2'
    TabOrder = 1
    object Edit3: TEdit
      Left = 12
      Top = 24
      Width = 121
      Height = 21
      TabOrder = 0
      Text = 'Edit3'
    end
    object Edit4: TEdit
      Left = 12
      Top = 64
      Width = 121
      Height = 21
      TabOrder = 1
      Text = 'Edit4'
    end
    object Button2: TButton
      Left = 8
      Top = 116
      Width = 75
      Height = 25
      Caption = 'Button2'
      TabOrder = 2
      OnEnter = Button2Enter
    end
  end
end
ASKER CERTIFIED SOLUTION
Avatar of calinutz
calinutz
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is just about setting the tab order for the controls on the 2 panels , and setting the OnEnter Event of the Control that you want to use to jump on the other panel.
My code jumps to the other panel when the button control gets focus. You can use any other control that has an OnEnter event.
Cheers  
Avatar of __alex
__alex

ASKER

Panels: P1, P2
Controls: C1..C6

+-----------------------+-----------------------+
! P1                          ! P2                         !
!                              !                              !
! C1                         ! C4                         !
! C2                         ! C5                         !
! C3                         ! C6                         !
!                              !                              !
+-----------------------+-----------------------+

Loop 1: C1 -> C2 -> C3 -> C1 (looping with tab key)
Loop 2: C4 -> C5 -> C6 -> C4
I believe alex meant something else.
The problem is just about setting TabStop property to false in the components you don't want to cycle through.
Here I wrote a procedure which should take care of everything for you:

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Panel2: TPanel;
    Label2: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    procedure Panel1Enter(Sender: TObject);
    procedure Panel2Enter(Sender: TObject);
  private
    { Private declarations }
    procedure SetCanCycleThroughControl(Cntrl:TWinControl;CanCycle:Boolean);
//Cntrl - control containing all childs that should (or shouldn't) be cycled through
//CanCycle - allow, dissallow cycling though the controls
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.SetCanCycleThroughControl(Cntrl:TWinControl;CanCycle:Boolean);
Var
i       :Integer;
begin
  for i:=0 to Self.ComponentCount-1 do begin
    if Self.Components[i] is TWinControl then
      if TWinControl(Self.Components[i]).Parent=Cntrl then
        TWinControl(Self.Components[i]).TabStop:=CanCycle;
  end;
end;

procedure TForm1.Panel1Enter(Sender: TObject);
begin
  SetCanCycleThroughControl(Panel1,true);
  SetCanCycleThroughControl(Panel2,false);
end;

procedure TForm1.Panel2Enter(Sender: TObject);
begin
  SetCanCycleThroughControl(Panel1,false);
  SetCanCycleThroughControl(Panel2,true);
end;

end.

Now, using SetCanCycleThroughControl procedure you can indicate any Panel (or even any TWinControl descendant) and allow (dissallow) cycling through all of it's child components using TabStop property.
For example - in the Panel1.OnPanelEnter event you indicate that all childs of that panel1 can be cycled through and of panel2 - cannot. Reversed for the Panel2.OnPanelEnter event.
Avatar of __alex

ASKER

Ok, got it. Thanks!
Avatar of __alex

ASKER

@Astral100
Sorry, but I closed the question before your comment appeared. Thank you anyhow!
Alex, could you please clarify which solution did you use?
Mine - Astral100 or calinutz's?

Your answer goes after my post, but the points go to the calinutz.
Could you clarify things a bit?
Ok, I see now.