Question about TDBGrid....

I have a table with three fields F1, F2 and F3, alphanumeric with length = 2. I am using a TDBGrid for editing. When editing a cell, I want to change to next cell (at right) when the max length is reached for this fields or when Enter key is pressed...how can I do it? (sorry, my english is not good)
markspinerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
Listening
0
RadlerCommented:
Hi Mark;

Again my prefered question type.

Use this
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, Grids, DBGrids, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Table1LAST_NAME: TStringField;
    Table1FIRST_NAME: TStringField;
    Table1ACCT_NBR: TFloatField;
    Table1ADDRESS_1: TStringField;
    Table1CITY: TStringField;
    Table1STATE: TStringField;
    Table1ZIP: TStringField;
    Table1TELEPHONE: TStringField;
    Table1DATE_OPEN: TDateField;
    Table1SS_NUMBER: TFloatField;
    Table1PICTURE: TStringField;
    Table1BIRTH_DATE: TDateField;
    Table1RISK_LEVEL: TStringField;
    Table1OCCUPATION: TStringField;
    Table1OBJECTIVES: TStringField;
    Table1INTERESTS: TStringField;
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
var
      Content : PChar;
      P, HWnd : THandle;
begin
      if Table1.State = dsEdit then begin
            HWnd:=Windows.GetFocus;
            P:=GetParent( HWnd );
            if P = DBGrid1.Handle then begin
                  Content:=StrAlloc( 255 );
                  SendMessage( HWnd, WM_GETTEXT, 255, integer(Content) );
                  if (DbGrid1.SelectedField.DataSize) < (Length(Content)+2) then begin
                        PostMessage( HWnd, WM_KEYDOWN, VK_TAB, 0 );
                  end;
                  StrDispose( Content );
            end;
      end;
end;

end.

///*********** The related DFM
object Form1: TForm1
  Left = 248
  Top = 103
  Width = 870
  Height = 640
  Caption = 'Form1'
  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 DBGrid1: TDBGrid
    Left = 56
    Top = 136
    Width = 745
    Height = 201
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    OnKeyPress = DBGrid1KeyPress
  end
  object Table1: TTable
    Active = True
    DatabaseName = 'DBDEMOS'
    TableName = 'CLIENTS.DBF'
    Left = 64
    Top = 8
    object Table1LAST_NAME: TStringField
      FieldName = 'LAST_NAME'
    end
    object Table1FIRST_NAME: TStringField
      FieldName = 'FIRST_NAME'
    end
    object Table1ACCT_NBR: TFloatField
      FieldName = 'ACCT_NBR'
    end
    object Table1ADDRESS_1: TStringField
      FieldName = 'ADDRESS_1'
    end
    object Table1CITY: TStringField
      FieldName = 'CITY'
    end
    object Table1STATE: TStringField
      FieldName = 'STATE'
      Size = 2
    end
    object Table1ZIP: TStringField
      FieldName = 'ZIP'
      Size = 5
    end
    object Table1TELEPHONE: TStringField
      FieldName = 'TELEPHONE'
      Size = 12
    end
    object Table1DATE_OPEN: TDateField
      FieldName = 'DATE_OPEN'
    end
    object Table1SS_NUMBER: TFloatField
      FieldName = 'SS_NUMBER'
    end
    object Table1PICTURE: TStringField
      FieldName = 'PICTURE'
      Size = 15
    end
    object Table1BIRTH_DATE: TDateField
      FieldName = 'BIRTH_DATE'
    end
    object Table1RISK_LEVEL: TStringField
      FieldName = 'RISK_LEVEL'
      Size = 8
    end
    object Table1OCCUPATION: TStringField
      FieldName = 'OCCUPATION'
    end
    object Table1OBJECTIVES: TStringField
      FieldName = 'OBJECTIVES'
      Size = 10
    end
    object Table1INTERESTS: TStringField
      FieldName = 'INTERESTS'
      Size = 120
    end
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 96
    Top = 72
  end
end

A special note about the part
if (DbGrid1.SelectedField.DataSize) < (Length(Content)+2) then begin
  PostMessage( HWnd, WM_KEYDOWN, VK_TAB, 0 );
end;
The condition used maybe dont be your desired condition, correct it OK ?
Good luck, and so long.
T++, Radler.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadlerCommented:
Hi !!

   ____(  \                .-''''''''''''''''-.             / )____
 (____     \______ /    (O   O)  \ _____/     ____)
 (____                   '-----(    )----'                _____)
 (____     _______  \  . \__/.  /______   ______)
  (_____/                  '-.____.-'               \_____)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.