Board index » delphi » Questions about TDBGrid, Enter key and Tab key.

Questions about TDBGrid, Enter key and Tab key.

I've created a form that contains some TDBEdit fields, a TDBGrid
that contains 7 columns (0-6), and a procedure that turns 'Enter' key as
'Tab' key.

In the procedure, if there were no data entered into the TDBGrid Field
[0], press 'Enter' will go to the next entry field, the TDBEdit field,
and this works fine.

My problem is, if data entered into the TDBGrid Field [0], it
still goes to the TDBEdit field but not TDBGrid Field [1]. And, if data
were
entered into the TDBGrid Field [6], it will then goes back to Field [0]
of the same row, but not the next row. But if 'Tab' key were pressed, it
will go to the next row.

Could someone show me how to solve this or is there any articles from
Inprise that related to this?

Many thanks,

AF

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
  if not (ActiveControl is TDBGrid) then begin
    Key := #0;
    Perform(WM_NEXTDLGCTL, 0, 0);
  end
  else if (ActiveControl is TDBGrid) then
    with TDBGrid(ActiveControl) do begin
      with Fields[0] do
      if IsNull then begin
        SelectNext(ActiveControl as tWinControl, True, True );
        Key := #0;
      end
      else begin
        if selectedindex = 6 then
          selectedindex := 0
//      *****************************
//      Key := #0;
//      Perform(WM_NEXTDLGCTL, 0, 0);
//      (I've tried the above code, but it does not work)
//      *****************************
        else if selectedindex < (fieldcount -1) then
            selectedindex := selectedindex +1;
      end;
    end;
end;

 

Re:Questions about TDBGrid, Enter key and Tab key.


Quote
> I've created a form that contains some TDBEdit fields, a TDBGrid
> that contains 7 columns (0-6), and a procedure that turns 'Enter' key as
> 'Tab' key.
> In the procedure, if there were no data entered into the TDBGrid Field
> [0], press 'Enter' will go to the next entry field, the TDBEdit field,
> and this works fine.
> My problem is, if data entered into the TDBGrid Field [0], it
> still goes to the TDBEdit field but not TDBGrid Field [1]. And, if data
> were
> entered into the TDBGrid Field [6], it will then goes back to Field [0]
> of the same row, but not the next row. But if 'Tab' key were pressed, it
> will go to the next row.

Make sure dgTabs is included in the dbgrids Options and replace your
current keypress handler with this one for CM_DIALOGKEY:

    // form declaration, private section
    Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;

Procedure TForm1.CMDialogKey(Var Msg: TWMKEY);
Begin
  If not (ActiveControl Is TButton) Then
    If Msg.Charcode = VK_RETURN Then
      Msg.Charcode := VK_TAB;
  inherited;
End;

This actually turns an enter key into a tab and may thus work better in
your scenario.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Re:Questions about TDBGrid, Enter key and Tab key.


Peter,

Is there anything else that must be done to make this work? I tried adding
this to my project and it seems to work for everything except the DBGrid.

FWIW, I'm using Delphi 4 C/S (5.104).

--
Ken Rachynski
Database Administrator
krac...@cadvision.com
<http://www.cadvision.com/krachyn>
ICQ: 3113514

Quote
Peter Below <100113.1...@compuserve.com> wrote in message ...

>Make sure dgTabs is included in the dbgrids Options and replace your
>current keypress handler with this one for CM_DIALOGKEY:

>    // form declaration, private section
>    Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;

>Procedure TForm1.CMDialogKey(Var Msg: TWMKEY);
>Begin
>  If not (ActiveControl Is TButton) Then
>    If Msg.Charcode = VK_RETURN Then
>      Msg.Charcode := VK_TAB;
>  inherited;
>End;

>This actually turns an enter key into a tab and may thus work better in
>your scenario.

>Peter Below (TeamB)  100113.1...@compuserve.com)
>No e-mail responses, please, unless explicitely requested!

Re:Questions about TDBGrid, Enter key and Tab key.


Quote
In article <77jb0e$sm...@forums.borland.com>, Ken Rachynski wrote:
> Is there anything else that must be done to make this work? I tried adding
> this to my project and it seems to work for everything except the DBGrid.

In fact it would fail to work for any control that handles the return key
itself, like a TMemo with WantReturn = true, or a TRichedit. I think a
TDBGrid also handles it, for it you can trap it in the grids OnKeyDown
handler, i think.

If you prefer a "central" solution try to handle the CM_CHILDKEY message
instead of CM_DIALOGKEY. That would look like this:

   private
    procedure cmChildKey( var msg: TWMKEY );
      message CM_CHILDKEY;

procedure tform1.cmChildKey( var msg: TWMKEY );
begin
  if msg.charcode = VK_RETURN then
    if not (Assigned(ActiveControl) and
            ((ActiveControl Is TButton) or
             (ActiveControl Is TCustomMemo)))
    then begin
      // swallow the key
      msg.result := 1;
      // fake a tab
      keybd_event( VK_TAB, MapVirtualKey( VK_TAB,0), 0, 0);
      keybd_event( VK_TAB, MapVirtualKey( VK_TAB,0), KEYEVENTF_KEYUP, 0);
    end;
end;

If there are more controls that you need to see the enter key you would
have to extend the if clause appropriately.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Re:Questions about TDBGrid, Enter key and Tab key.


Peter,

I did some further investigation and found out just why changing the Key
from VK_RETURN to VK_TAB does not work in the DBGrid and, by extension, the
Grid.

It turns out that default behaviour in the DBGrid is to have VK_RETURN put
you into EditMode and displays a TDBInPlaceEdit control. This control is a
descendent of TInPlaceEdit which has the following logic for its KeyDown
handler: process various key presses (VK_LEFT, VK_RIGHT, etc.) including
VK_TAB otherwise call ParentEvent and the inherited handler. ParentEvent
calls the user defined KeyDown handler.

So what we're seeing is that VK_TAB is processed *before* our event handler
ever sees the key. At this point, it doesn't matter much what you change the
key to. This is true only if the DBGrid.Options includes dgAlwaysShowEditor.
If you remove this option, you *can* use your handler to change an enter
into a tab, however, it takes two enter presses to tab through each field.
If you remove dgEditing from the options, this disables the in place editor
altogether and turns your grid into a view only grid and allows your handler
to turn the enter key into a fully realised tab key.

Quote
Peter Below wrote:
> In article <77jb0e$sm...@forums.borland.com>, Ken Rachynski wrote:
> > Is there anything else that must be done to make this work? I tried adding
> > this to my project and it seems to work for everything except the DBGrid.

> In fact it would fail to work for any control that handles the return key
> itself, like a TMemo with WantReturn = true, or a TRichedit. I think a
> TDBGrid also handles it, for it you can trap it in the grids OnKeyDown
> handler, i think.

> If you prefer a "central" solution try to handle the CM_CHILDKEY message
> instead of CM_DIALOGKEY. That would look like this:

<snip>

Quote

> If there are more controls that you need to see the enter key you would
> have to extend the if clause appropriately.

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No e-mail responses, please, unless explicitely requested!

--
Ken Rachynski
Database Analyst/Developer
krac...@cadvision.com
http://www.cadvision.com/krachyn

Other Threads