我从TDBGrid继承了一个MyDBGrid,并改写了一下KeyDown事件,然而当我把dgAlwaysShowEditor改为True时,KeyDown事件就

  • 主题发起人 主题发起人 maple_guo
  • 开始时间 开始时间
M

maple_guo

Unregistered / Unconfirmed
GUEST, unregistred user!
我从TDBGrid继承了一个MyDBGrid,并改写了一下KeyDown事件,然而当我把dgAlwaysShowEditor改为True时,KeyDown事件就不响应了,请帮忙看一下。(50分)<br />以下是我的Unit中的代码:
代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ADOColorDBGrid, DBGridEh, Grids, DBGrids, DB, ADODB;

type
  TMyDBGrid = class(TDBGrid)
    procedure KeyDown(var Key: Word;
      Shift: TShiftState); override;
  end;

  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MyDBGrid1: TMyDBGrid;

implementation

{$R *.dfm}

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 13 then
  begin
    Key := 0;
    PostMessage(DBGrid1.Handle, WM_KEYDOWN, VK_TAB, 0);
  end
  else
    inherited;
end;

{ TMyDBGrid }

procedure TMyDBGrid.KeyDown(var Key: Word;
  Shift: TShiftState);
begin
  if Key = 13 then
  begin
    Key := 0;
    PostMessage(Handle, WM_KEYDOWN, VK_TAB, 0);
  end
  else
    inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyDBGrid1 := TMyDBGrid.Create(Self);
  with MyDBGrid1 do
  begin
    Parent := Self;
    DataSource := DataSource1;
    Align := alClient;
    Options := DBGrid1.Options; ///////
    Show;
  end;
end;

end.

DBGrid1的OnKeyDown事件正常响应;

如果把DBGrid1的Options-&gt;dgAlwaysShowEditor设为True,MyDBGrid1的KeyDown就不响应了,反之则正常响应。为什么会这样???不解[:(][:(]
 
因为editing时是TEdit接收到信息的
 
To sky2008:
Grids.pas的源码我也看了,我的KeyDown消息的确是被InplaceEditor给吃掉了,问题是出在这个过程中:
代码:
  procedure ParentEvent;
  var
    GridKeyDown: TKeyEvent;
  begin
    GridKeyDown := Grid.OnKeyDown;
    if Assigned(GridKeyDown) then GridKeyDown(Grid, Key, Shift);
  end;
那我岂不是没有办法了吗?消息被先吃掉了,我的MyDBGrid根本就得不到消息[:(]
 
dgAlwaysShowEditor The grid is always in edit mode. That is, the user does not have to press Enter or F2 before editing the contents of a cell. dgAlwaysShowEditor does nothing unless dgEditing is also included in Options. dgAlwaysShowEditor is ignored if Options includes dgRowSelect.

同时设置一下:dgEditing
 
问题已经解决了,我改了一下Grids.pas的源码。
 
后退
顶部