Board index » delphi » TAB key and Field validation

TAB key and Field validation

Does anyone know how to intercept de TAB key?

i'm trying to intercept the TAB and RETURN key to complete an OnKeyDown
validation
soo far, i'm only able to get the return, but the TAB key dont seem to
generate a OnKeyDOwn event.
Anyone know why.... or how to solve this?

Here the procedure:

procedure TfrmClients.No_ClientKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
Var
  Validate : TQuery;
  Duplicate: String;
begin
  Case Key Of
    Vk_Return,                // The RETURN KEY works well
    Vk_Tab: Begin           // The TAB KEY don't seem to generate this event
               If Length(Trim(No_Client.Text)) = 0 Then
                  Begin
                     MessageDLG('Un numro de client est requis pour crer
une nouvelle fiche!',mtWarning,[mbOK],0);
                     Exit;
                  End;
               Validate := TQuery.Create(self);
               Validate.SQL.Add('Select No_Client From Tables\Clients.db
Where Trim(Upper(No_Client)) = :NoClient');
               Validate.ParamByName('NoClient').AsString :=
Trim(UpperCase(No_Client.Text));
               Validate.Open;

               Duplicate := Trim(UpperCase(No_Client.Text));

               If Validate.RecordCount > 0 Then
                  Begin
                     Case dmNova.tblClients.State Of
                        dsInsert: MessageDlg('Ce client existe
dj!',mtWarning,[mbOk],0);
                     End;
                     dmNova.tblClients.Cancel;
                     dmNova.tblClients.Locate('No_Client',Duplicate,[]);
                     Exit;
                  End
               Else
                  Begin
                     Case Length(Trim(dmNova.tblClientsNo_Client.Value)) Of
                        7, 10: dmNova.tblClientsTel_Bur.Value :=
dmNova.tblClientsNo_Client.Value;
                     End;
                     Prefixe.SetFocus;
                  End;
            End;
  End;
end;

 

Re:TAB key and Field validation


This is one way to do it. Perhaps there are beter ways, but it works.

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  if Msg.message = WM_KEYDOWN then
  begin
    if Msg.wParam=VK_TAB then
    Begin
      MessageDLG('Un numro de client est requis pour crer une nouvelle
fiche!',mtWarning,[mbOK],0);
      Handled := True;
    end
    else
      Handled := False;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage:=AppMessage;
end;

--
--------------------------
Regards,

Mathias Nyman
Sweden
http://www.se-datateknik.se/delphi
zio...@hem.passagen.se.nospam
--------------------------

Michel Hudon <hud...@globetrotter.qc.ca> skrev i
diskussionsgruppsmeddelandet:xZTp4.2516$H07.64...@news.globetrotter.net...

Quote
> Does anyone know how to intercept de TAB key?

> i'm trying to intercept the TAB and RETURN key to complete an OnKeyDown
> validation
> soo far, i'm only able to get the return, but the TAB key dont seem to
> generate a OnKeyDOwn event.
> Anyone know why.... or how to solve this?

Re:TAB key and Field validation


The message <xZTp4.2516$H07.64...@news.globetrotter.net>
  from  "Michel Hudon" <hud...@globetrotter.qc.ca> contains these words:

Quote
> Does anyone know how to intercept de TAB key?

Michael,

The tab key gets translated to a system message.
Take a look at WM_SYSCOMMAND in the win32 stuff.
In the context of what you are doing it would be
the same as the SC_NEXTWINDOW command.

The tab key moves the keyboard focus away from
the current control and on to the next one in
the tab order. You can detect when focus moves
away from a control using the control's On Exit
event. A lot of people use the On Exit event for
validation.

Hope this helps

--

regards Andreas Kyriacou
http://www.andrikkos.co.uk

Re:TAB key and Field validation


Quote
Michel Hudon wrote:

> Andreas Kyriacou <andrik...@zetnet.co.uk> a crit dans le message :
> 2000021512112080...@zetnet.co.uk...
> > The message <xZTp4.2516$H07.64...@news.globetrotter.net>
> >   from  "Michel Hudon" <hud...@globetrotter.qc.ca> contains these words:

> > > Does anyone know how to intercept de TAB key?

> > Michael,

> > The tab key gets translated to a system message.
> > Take a look at WM_SYSCOMMAND in the win32 stuff.
> > In the context of what you are doing it would be
> > the same as the SC_NEXTWINDOW command.

> > The tab key moves the keyboard focus away from
> > the current control and on to the next one in
> > the tab order. You can detect when focus moves
> > away from a control using the control's On Exit
> > event. A lot of people use the On Exit event for
> > validation.

> I c, then how can i stop it from completing the control exit?
> cause i think that when the OnExit event is reach, the control as already
> lost focus.
> How can i stop the event and refocus to the exiting control without having
> to write an OnEnter even?

fieldname.setfocus in your event

But anyway you're trying to catch the TAB key?!
The key only shift focus, it doesn't mean that a value has been entered.

Why not an validation rule in your database?
Or a check in the dataset.post event?
Before post event: if field.text = null or field.text = '' then
message...

--
greetings Ton

Re:TAB key and Field validation


The message <NFjq4.2614$H07.68...@news.globetrotter.net>
  from  "Michel Hudon" <hud...@globetrotter.qc.ca> contains these words:

Quote
> I c, then how can i stop it from completing the control exit?
> cause i think that when the OnExit event is reach, the control as already
> lost focus.
> How can i stop the event and refocus to the exiting control without having
> to write an OnEnter even?

Hi Michael,

Just set the focus back to the control in the controls own OnExit event.
for example if the control is an edit box: Edit1, and you don't want
the user to move away from the control leaving it blank then in the
OnExit method of Edit1 you say:-

       if Edit1.Text = '' then Edit1.SetFocus;

This will override the users attempt to move focus away from Edit1
He has to enter something in the box or focus will stay on Edit1.

--
regards Andreas Kyriacou
http://www.andrikkos.co.uk

--------------------------
Don't criticize a man until you've walked a mile in his shoes,
that way when you criticize him you're a mile away and have his shoes.

possibly Graucho Marks, Anybody know?

Other Threads