?
Solved

Question about TDBGrid....

Posted on 2000-02-08
3
Medium Priority
?
416 Views
Last Modified: 2010-08-05
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)
0
Comment
Question by:markspiner
  • 2
3 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2501373
Listening
0
 
LVL 4

Accepted Solution

by:
Radler earned 400 total points
ID: 2505420
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
 
LVL 4

Expert Comment

by:Radler
ID: 2520354
Hi !!

   ____(  \                .-''''''''''''''''-.             / )____
 (____     \______ /    (O   O)  \ _____/     ____)
 (____                   '-----(    )----'                _____)
 (____     _______  \  . \__/.  /______   ______)
  (_____/                  '-.____.-'               \_____)
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question