I've been fighting this crazy problem for hours and have gotten nowhere. I have this problem in two completely different projects using a TCollection. When a new collection item is added, I need to initialize the values of that item. However, they're not defaulting at all. I'm even setting them in two completely different places, in the item's constructor, and in the collection's add function - neither of them are working. I can set the values once the items are there, but I need to set default values. I've done collections in the past and never had this problem, I must be missing something here...
(Removed component and everything else from unit, there are a few references to this component so it won't compile)
JD Solutions
(Removed component and everything else from unit, there are a few references to this component so it won't compile)
Code:
unit JDGrids;
interface
uses
Classes, Windows, SysUtils, Grids, StrUtils;
type
TJDGridCol = class;
TJDGridCols = class;
TJDGridCols = class(TCollection)
private
fOnEvent: TNotifyEvent;
private
fOwner: TComponent;
procedure DoEvent;
property OnEvent: TNotifyEvent read fOnEvent write fOnEvent;
protected
function GetItem(Index: Integer): TJDGridCol;
procedure SetItem(Index: Integer; Value: TJDGridCol);
function GetOwner: TPersistent; override;
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
function Add: TJDGridCol;
procedure Assign(Source: TPersistent); override;
procedure Clear;
procedure Delete(Index: Integer);
property Items[Index: Integer]: TJDGridCol read GetItem write SetItem; default;
end;
TJDGridCol = class(TCollectionItem)
private
fOwner: TComponent;
fWidth: Integer;
fTitle: String;
fCols: TJDGridCols;
fOnEvent: TNotifyEvent;
fVisible: Bool;
procedure SetTitle(const Value: String);
procedure SetWidth(const Value: Integer);
procedure DoEvent;
property OnEvent: TNotifyEvent read fOnEvent write fOnEvent;
procedure SetVisible(const Value: Bool);
protected
function GetDisplayName: String; override;
public
constructor Create(AOwner: TJDGridCols);
destructor Destroy; override;
published
property Title: String read fTitle write SetTitle;
property Width: Integer read fWidth write SetWidth;
property Visible: Bool read fVisible write SetVisible;
end;
implementation
{ TJDGridCols }
constructor TJDGridCols.Create(AOwner: TComponent);
begin
inherited Create(TJDGridCol);
fOwner:= AOwner;
end;
destructor TJDGridCols.Destroy;
begin
inherited Destroy;
end;
function TJDGridCols.Add: TJDGridCol;
begin
Result:= TJDGridCol(inherited Add);
Result.fCols:= Self;
Result.fTitle:= 'Column '+IntToStr(Result.ID);
Result.fWidth:= 30;
Result.fVisible:= True;
DoEvent;
end;
procedure TJDGridCols.Assign(Source: TPersistent);
begin
inherited Assign(Source);
DoEvent;
end;
procedure TJDGridCols.Clear;
begin
inherited Clear;
DoEvent;
end;
procedure TJDGridCols.Delete(Index: Integer);
begin
inherited Delete(Index);
DoEvent;
end;
function TJDGridCols.GetItem(Index: Integer): TJDGridCol;
begin
Result:= TJDGridCol(inherited Items[Index]);
end;
function TJDGridCols.GetOwner: TPersistent;
begin
Result:= fOwner;
end;
procedure TJDGridCols.SetItem(Index: Integer; Value: TJDGridCol);
begin
inherited Items[Index]:= Value;
DoEvent;
end;
procedure TJDGridCols.DoEvent;
begin
if assigned(fOnEvent) then fOnEvent(Self);
end;
{ TJDGridCol }
constructor TJDGridCol.Create(AOwner: TJDGridCols);
begin
inherited Create(AOwner);
fOwner:= AOwner.fOwner;
fCols:= AOwner;
fTitle:= 'Column '+IntToStr(ID);
fWidth:= 30;
fVisible:= True;
end;
destructor TJDGridCol.Destroy;
begin
inherited Destroy;
end;
procedure TJDGridCol.DoEvent;
begin
if assigned(fOnEvent) then fOnEvent(Self);
end;
function TJDGridCol.GetDisplayName: String;
begin
Result:= fTitle;
end;
procedure TJDGridCol.SetTitle(const Value: String);
begin
fTitle:= Value;
DoEvent;
end;
procedure TJDGridCol.SetVisible(const Value: Bool);
begin
fVisible := Value;
DoEvent;
end;
procedure TJDGridCol.SetWidth(const Value: Integer);
begin
fWidth := Value;
DoEvent;
end;
end.
JD Solutions