程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> DBGrid中用光標鍵控制Cell

DBGrid中用光標鍵控制Cell

編輯:Delphi

  {////////////////////////
  //                     //
  //     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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved