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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can the Tab key be trapped in a TEdit event?

Status
Not open for further replies.

dba2

Programmer
May 23, 2002
16
GB
I have created a form containing 4 TEdit boxes and have written code to validate the text entered. I want to be able to move back with Shift-Tab and forward with Tab between the edit boxes if validation is correct. I do not seem to be able to trap the Tab key through any of the TEdit events.

Can anyone help please?

dba2
 
You can do this simply by setting the TabStop property of each control in the order you wish to move.

No custom code is required, and no events need to be caught. The TEdit takes care of it for you.

good luck,
offrdbandit
 
Thanks for the response, unfortunately I neglected to say that each following Edit box is disabled until the preceding one's entry has been validated as correct, hence the reason for the need to trap the Tab.

Any other ideas please?
 
Set 'KeyPreview' on your main form to true and then use KeyDown/Up in form to do your stuff.
Try, it might help, not completely sure about this...
 
Thanks bNasty I'll try it. In the meantime it looks as if the same problem exists in Delphi for which I've found a solution by Thomas Stutz which follows:
----------------------------------------------------------
Problem/Abstract: It's not possible to trap the tabulator-key in the
OnkeyPress/OnkekDown/OnkeyUp handler of a TEdit control.
Here are two ways to trap it!

private
Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;

{...}

Procedure TForm1.CMDialogKey(Var Msg: TWMKEY);
Begin
If (ActiveControl = Edit1) Then
If Msg.Charcode = VK_TAB Then
begin
ShowMessage('Tab');
{ Msg.Charcode := 0; } // to eat the tab key!
end;
inherited;
End;


{**** Or/ Oder: ****}

private
Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;

{...}

procedure TForm1.CMDialogKey(var Msg: TWMKey);
begin
if (Edit1.Focused) then
Msg.Result := 0
else
inherited;
end;


procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Ord(Key) = VK_TAB then
begin
ShowMessage('Tab');
Key := #0; // no beep!
end;
end;

{**** Or/ Oder: ****}
(by Peter Larson)

{By adding a message handler for WM_GETDLGCODE to a component,
it is possible for a component to capture the tab key.
The tab key will be passed to the OnKeyDown event handler like other keys.
The following D4 code shows what needs to be added to a component to allow
the capture of the tab key.}

Type
TExtEdit = Class(tEdit)
Private
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
End;

{...}

procedure TExtEdit.WMGetDlgCode(var Message: TWMGetDlgCode);
Begin
Inherited;
Message.Result := Message.Result or DLGC_WANTTAB;
End;

--------------------------------------------------------
Does anyone know how they can be re-written for C++Builder please?
 
set all edit boxes to this event handler;

void __fastcall Ttable::Edit1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (Key == VK_TAB)
{
if ((TEdit)sender = Edit1)
{
Edit2->Enabled = true;
// or some other code
}
if ((TEdit)sender = Edit2)
{
Edit3->Enabled = true;
// or some other code
}
}
}

the logic is not neccessarily what you need but maybe you get the idea.

tomcruz.net
 
Have you considered this ?

On each Edit box there is an event called "OnExit".. why not put your validation of the content in this event and then decide whether to progress to the next box...

Consider a form ( Form1 ) with 2 text boxes ( Edit1,Edit2 ) and you need to make sure it's 6 characters long before you can let the user go to the next field...

void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
if ( Edit1->Text.Length() != 6 )
Edit1->SetFocus();
}

The code executes as the focus leaves the Edit1 box. at that point it is validated and if it passes the condition then it is "allowed" to leave , else it gets dragged back to the same control by the SetFocus() method. Hence the user cannot leave that field until the data is valid.

Hope this helps


Regards


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top