How do I respond to a resize event in my custom grid control?

拜拜、爱过 提交于 2020-01-02 05:12:07

问题


I am new to Delphi and I'm building a custom control derived from TStringGrid. I need access to the OnResize event handler. How do I get access to it? TStringGrid's parent has a OnResize event


回答1:


Publish the OnResize event, which is protected by default in TControl.


In an own descendant, you should not use the event itself, but rather the method that triggers the event. Doing it that way will give the users of your component the opportunity to implement an own event handler.

Override the Resize method:

type
  TMyGrid = class(TStringGrid)
  protected
    procedure Resize; override;
  published
    property OnResize;
  end;

{ TMyGrid }

procedure TMyGrid.Resize;
begin
  // Here your code that has to be run before the OnResize event is to be fired
  inherited Resize; // < This fires the OnResize event
  // Here your code that has to be run after the OnResize event has fired
end;



回答2:


Overriding Resize has a problem: the event will be called not only when the grid is actually resized but also when you change the RowCount.

In one program I needed to add/delete rows to grid as the user resized the grid. In other words I had to keep the grid filled with rows/data. However, data was not accessible while I was adding/deleting rows.

So, I used this:

protected
   procedure WMSize(var Msg: TWMSize); message WM_SIZE;
   .....

Implementation

procedure TMyGrid.WMSize(var Msg: TWMSize);      
begin
 inherited;
 if (csCreating in ControlState) then EXIT;                                                          { Maybe you also need this } { Don't let grid access data during design }
 ComputeRowCount;
 AssignDataToRows;
end;



回答3:


You could simply place the TStringGrid inside a TPanel and align it to alClient, then use the published Resize event of the TPanel for any actions.



来源:https://stackoverflow.com/questions/14727765/how-do-i-respond-to-a-resize-event-in-my-custom-grid-control

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