Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TTabControl switching with <CTRL>-<TAB>

Status
Not open for further replies.

masonwheeler

Programmer
Aug 14, 2007
3
US
I put a TTabControl on my form, and to my great surprise, I'm not able to switch between tabs with <CTRL>-<TAB> at runtime. I thought this was default behavior; every app I've ever used with a tabbed control will do this. How do I implement the behavior, so that if I press <CTRL>-<TAB> (or <CTRL>-<SHIFT>-<TAB>) in a form with a TTabControl on it, no matter which component currently has the focus, the TTabControl will move to the next (previous) tab and call it's onChange event?

Mason
 
strange request, but it can be done.

this will move focus between controls, as soon as the tab gets focus it will change tabs.

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    TabControl1: TTabControl;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  CurrentTab: Integer;

implementation

{$R *.DFM}
procedure TForm1.CMDialogKey(var Msg: TWMKey);
begin
 if (TabControl1.Focused) then
    Msg.Result := 0
 else
 inherited;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (ssShift in Shift) and (Ord(Key) = VK_TAB) then
 begin
  if CurrentTab>0 then
   if Ord(Key) = VK_TAB then TabControl1.TabIndex:=CurrentTab-1;
   CurrentTab:=TabControl1.TabIndex;
   exit;
 end;

if Ord(Key) = VK_TAB then
 if CurrentTab<TabControl1.Tabs.Count then
  TabControl1.TabIndex:=CurrentTab+1;
 CurrentTab:=TabControl1.TabIndex;
end;

end.
[\code]

Aaron
 
woops, i just saw that you want ctrl tab.
anyways you should be able to modify what ive posted.

Aaron
 
Interesting. I'm reading over the code, and I understand most of it, but what does the CMDialogKey procedure do? Messages are mostly a mystery to me, and I don't see what this one's doing.

Also, how do you "enable form keypreview"? (And if that's what I think it is, it might solve another problem I've been having, so knowing how to do it would be quite helpful to me.)

Mason
 
When you have the form selected in the Design, under the Input section is the KeyPreview property.
=)

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
I managed to get it working. Aaron's code didn't work completely (maybe he's using a different version of Delphi?) but it gave me a place to start from. Also, the Message thing did nothing whatsoever (as far as I can tell) and I ended up removing it.

Here's what I ended up with, in case anyone wants to see a working solution for BDS '06.

procedure TfrmMyForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssCtrl in Shift) and (Ord(Key) = VK_TAB) then
begin
if ssShift in Shift then
if TabControl.tabIndex > 0 then
TabControl.TabIndex := TabControl.tabIndex - 1
else
TabControl.TabIndex := TabControl.tabs.Count - 1
//end if
else
if TabControl.tabIndex < TabControl.Tabs.Count - 1 then
TabControl.TabIndex := TabControl.tabIndex + 1
else
TabControl.TabIndex := 0;
//end if
//end if
FocusControl(TabControl);
TabControl.OnChange(self);
end;
end;

Thanks for the help!

Mason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top