2

我有一个 TDBGrid 组件。我需要捕捉在调整网格列大小时触发的事件。

4

2 回答 2

3

获得事件的唯一地方似乎是覆盖 ColWidthChanged ...

type
  TDBgrid=Class(DBGrids.TDBGrid)
       private
       FColResize:TNotifyEvent;
       procedure ColWidthsChanged; override;
       protected
       Property OnColResize:TNotifyEvent read FColResize Write FColResize;
  End;

  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    DBGrid1: TDBGrid;
    ADODataSet1: TADODataSet;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
  private
    procedure ColResize(Sender: TObject);
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TDBgrid }

procedure TDBgrid.ColWidthsChanged;
begin
  inherited;
  if Assigned(FColResize) then  FColResize(self);

end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  DBgrid1.OnColResize := ColResize;
end;

procedure TForm1.ColResize(Sender:TObject);
begin
  Caption := FormatDateTime('nn:zzz',now)  ;
end;
于 2013-02-25T10:11:00.870 回答
3

您需要创建 TDBGrid 的后代并自己实现事件。像这样的东西:

unit MyDBGrid;

interface

type
  TMyDBGrid = class(TDBGrid)
  private
    FOnColResize: TNotifyEvent;
  protected
    procedure ColWidthsChanged; override;
  public
  published
    property OnColResize: TNotifyEvent read FOnColResize write FOnColResize;
  end;

implementation

{ TMyDBGrid }

procedure TMyDBGrid.ColWidthsChanged;
begin
  inherited;
  if (Datalink.Active or (Columns.State = csCustomized)) and
    AcquireLayoutLock and Assigned(FOnColResize) then
    FOnColResize(Self);
end;

end.

这应该可以,我现在没有时间测试它。

于 2013-02-25T10:19:41.783 回答