Where the TDBGrid Columns resize event was triggered

时光毁灭记忆、已成空白 提交于 2019-12-24 00:15:38

问题


I have a TDBGrid component. I need to catch the event triggered when I'm resizing a column of the grid.


回答1:


the only place to get an events seems to be overriding 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;



回答2:


you need to create a descendent of TDBGrid and implement the event by yourself. Something like this:

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.

this should work, I don't have time now to test it.



来源:https://stackoverflow.com/questions/15063680/where-the-tdbgrid-columns-resize-event-was-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!