Blue
Blue

Reputation: 43

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

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

Upvotes: 4

Views: 3083

Answers (3)

IceCold
IceCold

Reputation: 21134

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;

Upvotes: 2

user3210861
user3210861

Reputation: 23

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.

Upvotes: 0

NGLN
NGLN

Reputation: 43649

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;

Upvotes: 10

Related Questions