Mirror

Determine if ColCount has changed in a TStringGrid (Views: 101)


Problem/Question/Abstract:

I'm writing a TStringGrid descendant in which I would like to know, if the ColCount is being changed, because I have some other objects hidden in the component, that should be updated, when the ColCount changes. How can I accomplish that?

Answer:

You can try the following:

unit MyStringGrid;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids;

type
  TMyStringGrid = class(TStringGrid)
  private
    FColCount: Integer;
    FOnColCountChanged: TNotifyEvent;
    procedure SetColCount(Value: Integer);
  protected
    procedure ColCountChanged; virtual;
  public
    constructor Create(aOwner: TComponent); override;
  published
    property ColCount: Integer read FColCount write SetColCount default 5;
    property OnColCountChanged: TNotifyEvent read FOnColCountChanged
      write FOnColCountChanged;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Test', [TMyStringGrid]);
end;

constructor TMyStringGrid.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  FColCount := 5;
end;

procedure TMyStringGrid.SetColCount(Value: Integer);
begin
  if FColCount <> Value then
  begin
    FColCount := Value;
    inherited ColCount := FColCount;
    ColCountChanged;
  end;
end;

procedure TMyStringGrid.ColCountChanged;
begin

  {do dependend stuff here}

  if Assigned(FOnColCountChanged) then
    FOnColCountChanged(Self);
end;

end.

<< Back to main page