TStringGrid et Objects[aCol aRow]
Bonjour,
Suite à diverses discussions engagées récemment sur ce forum, j'ai regardé l'utilisation de TStringGrid et Objects[aCol, aRow]. J'ai obtenu un code qui fonctionne mais je comprends mal sa mécanique.
Dans mon composant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| unit myStringGrid;
{$mode objfpc}{$H+}
TClassAddOn = class(TObject)
private
Selected : Boolean;
public
ID : string[20];
StrBlob : string;
// constructor Create(???); virtual;
end;
TmyStringGrid = class(TStringGrid)
private
{ Private declarations }
protected
{ Protected declarations }
procedure SizeChanged(OldColCount, OldRowCount: Longint); override;
public
{ Public declarations }
published
{ Published declarations }
end;
{constructor TClassAddOn.Create(????);
begin
Selected := False;
StrBlob:= '';
end; }
constructor TmyStringGrid.Create(AOwner: TComponent);
begin
Inherited Create(AOwner);
end;
destructor TmyStringGrid.Destroy;
begin
inherited Destroy;
end;
procedure TmyStringGrid.SizeChanged(OldColCount, OldRowCount: Longint);
var
i : integer;
begin
inherited;
if not (csDesigning in ComponentState) then begin
if OldRowCount < RowCount then
for i := OldRowCount to RowCount -1 do
if TClassAddOn(Objects[0,i]) = nil then Objects[0,i] := TClassAddOn.Create();
end;
end;
end. |
Utilisé ainsi par exemple de la Form,
Code:
1 2 3 4 5 6
| begin
with myStringGrid1 do begin
for i := FixedRows to RowCount -1 do
Cells[2,i] := TClassAddOn(Objects[0,i]).StrBlob;
end;
end; |
Alors plusieurs questions :
- Cela fonctionne sans le constructeur pour TClassAddOn. Est-ce normal ?
- En supposant que l'on utilise un constructor TClassAddOn.Create(???), comment devrait-il être déclaré ?
- Lors des appels à DeleteRows, le TClassAdOn[Objects[0,Arow]).Free est exécuté automatiquement. Normal ?
- Le corollaire à ces 3 questions : est-ce pour cela qu'il me faut déclarer if TClassAddOn(Objects[0,i]) = nil then Objects[0,i] := TClassAddOn.Create(); dans TmyStringGrid.SizeChanged ?
En résumé, je suis dans les clous ou à côté ? Et question subsidiaire et habituelle : où trouve-t-on de la Doc Lazarusienne... J'aimerais un peu structurer mes connaissances éparses en la matière. J'apprécie l'empirisme mais, même si j'ai la chance de disposer de pas mal de temps, il me semble inutile dans ce cas précis de le gaspiller.
Doc Lazarusienne...et pas nécessairement "Delphienne", car par exemple, le code fourni par Mick605 (que je salue en passant) fonctionne probablement sous Delphi -si j'ai bien compris les quelques Posts que j'ai lus ici et là- mais pas sous Lazarus (ça c'est une certitude ;))
Citation:
Envoyé par
mick605
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| TAnObject = class
private
FMonBoolean : boolean;
FData : TObject;
public
property MonBoolean : boolean read FMonBoolean write FMonBoolean;
property Data : TObject read FData write FData ;
end;
TMyStringGrid = class
private
function GetObject(ACol, ARow : integer):TObject;
procedure SetObject(ACol, ARow : integer; AObject : TObject);
public
property Objects [ACol, ARow : integer] : TObject read GetObject write SetObject;
end;
...
function TMyStringGrid.GetObject(ACol, ARow : integer):TObject;
begin
Result:=TAnObject(TMyStringGrid.Objects[ACol,ARow]).Data;
end;
procedure TMyStringGrid.SetObject(ACol, ARow : integer; AObject : TObject);
begin
TAnObject(TMyStringGrid.Objects[ACol,ARow]).Data:=AObject;
end; |
L'erreur est provoquée par l'utilisation de TAnObject(TMyStringGrid.Objects[ACol,ARow]) pour être plus précis par le TMyStringGrid en lignes 22 et 27. Pourquoi ?
Cordialement. Gilles