首页 > 学院 > 开发设计 > 正文

DBGrid中用光标键控制Cell

2019-11-18 18:03:23
字体:
来源:转载
供稿:网友
 

{////////////////////////
//                     //
//     Grids549        //
//                     //
//   14:33 2005-2-17   //
//                     //
}////////////////////////
unit Grids549;

interface

uses
  DBGrids, Classes, Windows;

type
  TDBGrid549 = class(TDBGrid)
  public
    PRoperty InPlaceEditor;
  end ;
 
procedure CursorCtrl(Sender: TObject; var Key: Word; Shift: TShiftState); 

implementation

{ TDBGrid549 }

procedure CursorCtrl(Sender: TObject; var Key: Word;
  Shift: TShiftState);
{
Warn: This procedure may change VK_RIGHT and VK_LEFT to 0

Call this in DBGridKeyDown procedrue
eg:
procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Grids549.CursorCtrl(Sender, Key, Shift);
end;
}
var
  GridEditorMode: Boolean;
begin
  if Shift <> [] then Exit;
  if (Key <> VK_RIGHT) and (Key <> VK_LEFT) then
    Exit;
  GridEditorMode := TDBGrid(Sender).EditorMode;
  if not GridEditorMode then
    TDBGrid(Sender).EditorMode := True;
 
  if Key = VK_RIGHT then begin
    if GridEditorMode and (TDBGrid549(Sender).InplaceEditor.SelStart <>
      Length(TDBGrid549(Sender).InplaceEditor.Text)) then begin
      Exit;
    end;
    //Last field, move to next record     
    if TDBGrid(Sender).SelectedIndex =
      TDBGrid(Sender).FieldCount - 1 then begin
      TDBGrid(Sender).DataSource.DataSet.Next;
      TDBGrid(Sender).SelectedIndex := 0;
    end
    else
    //Focus on next field
    if TDBGrid(Sender).EditorMode then begin
      TDBGrid(Sender).SelectedIndex := TDBGrid(Sender).SelectedIndex + 1;
    end;
  end
  else
  if Key = VK_LEFT then begin
    if GridEditorMode and(TDBGrid549(Sender).InplaceEditor.SelStart <> 0) then
      Exit;
    //First field, move to prior record
    if TDBGrid(Sender).SelectedIndex = 0 then begin
        TDBGrid(Sender).DataSource.DataSet.Prior;
        TDBGrid(Sender).SelectedIndex := TDBGrid(Sender).FieldCount - 1;
    end
    else
    //Focus on prior field
    if TDBGrid(Sender).EditorMode then begin
      TDBGrid(Sender).SelectedIndex := TDBGrid(Sender).SelectedIndex - 1;
    end;
  end;
  Key := 0;
end;

end.


上一篇:检查RS232(串口)是否接有设备

下一篇:一个很实用的Ehlib排序函数,适合ADO

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
学习交流
热门图片

新闻热点

疑难解答

图片精选

网友关注