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

I am new to Delphi and I am creating a custom control derived from TStringGrid. I need access to the OnResize event handler. How do I access it? TStringGrid Parent Has OnResize Event

+4
source share
3 answers

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


In your own descendant, you should not use this event, but rather a method that triggers the event. Performing this method will give users of your component the ability to implement their 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; 
+10
source

Overriding Resize has a problem: an event will be raised not only when the mesh is actually resized , but also when the RowCount changes .

In one program, I needed to add / remove rows to the grid, as the user changed the grid size. In other words, I had to keep the grid filled with rows / data. However, the data was not available when I added / deleted 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; 
+2
source

You can simply put the TStringGrid inside the TPanel and align it with alClient, and then use the published Resize event for the TPanel for any action.

0
source

All Articles