我正在尝试使用我的后代内的Stringgrid的Objects属性,我想我做错了。 因此,我创建了一个在StringGrid单元格中使用的简单类,如:
type
TKind = (tkNone,tkStart, tkEnd, tkMiddle);
TMyCell = class(TObject)
private
FKind:string; // TKind: start, middle, end, none
FOfType:string; // new, registered, paid, over, none
FActualDate:TDate;
FTheText:string; // if you want to show a text in it
FIsWeekend:Boolean;
function IsItWeekend(dt:Tdate):boolean;
procedure setKind(s:string);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create;
property Kind:string read FKind write setKind;
property OfType:string read FOfType write FOfType;
property ActualDate:TDate read FActualDate write FActualDate;
property TheText:string read FTheText write FTheText;
property IsWeekend:Boolean read FIsWeekend write FIsWeekend default false;
{ Public declarations }
published
{ Published declarations }
end;
implementation
procedure TMyCell.setKind(s:string);
begin
FKind:=s;
end;
function TMyCell.IsItWeekend(dt:Tdate):boolean;
begin
if (DayOfTheWeek(dt)=6) or (DayOfTheWeek(dt)=7) then IsItWeekend:=true else IsItWeekend:=false;
end;
constructor TMyCell.Create;
var
i:integer;
a,l,z:word;
dt:TDate;
begin
FKind:='none';
FOfType:='none';
FActualDate:=now;
FTheText:='';
FIsWeekend:=IsItWeekend(FActualDate);
end;
然后,在我的StringGrid后代(TMyGrid)中,我执行以下操作:
TMyGrid = class(TStringGrid)
private
FStartSelection:integer;
FFixedColor:TColor;
FRowCount:integer;
...
published
property ColCount;
property RowCount;
...
constructor TMyGrid.Create(AOwner: TComponent);
var
i:integer;
a,l,z:word;
dt:TDate;
j: Integer;
myCell:TMyCell;
begin
inherited;
...// different settings
RowCount:=5;
for i := 0 to colCount-1 do
for j := 0 to RowCount-1 do
begin
Objects[i, j] := TMyCell.Create;
end;
end;
destructor TMyGrid.Destroy;
var
i,j:integer;
begin
for i := 0 to colCount-1 do
for j := 0 to RowCount-1 do
begin
TMyCell(Objects[i, j]).Free;
end;
inherited;
end;
... // other stuff
procedure Register;
begin
RegisterComponents('mygrid', [TMyGrid]);
end;
问题是我不知道如何在开发人员在运行应用程序之前更改objectInspector中的RowCount时告诉我的控件有更多行。 所以我将StrinGrid后代放在一个表单上,并将rowCount设置为10.但是我的StringGrid没有为新行创建的对象,所以ARow = 5-> 9上的单元格没有创建对象...因为在OnCreate中我只将RowCount的初始值设置为5,并为i:= 0创建对象到RowCount-1。
在开发人员更改ObjectInspector中的rowCount后,是否有一个事件或方法可以告诉StringGrid创建对象?
我确信这是我的问题,因为使用上面的代码,当我将stringGrid放在一个表单上并将它的rowCount(设计时或运行时)设置为10时,我想为一个值赋值行上的单元格的类型属性> 4我得到一个AccessViolation,但是如果我为< = 4的行执行此操作,则赋值可以正常工作。
我发现了一些应该有用的内容:http://embarcadero.newsgroups.archived.at/public.delphi.ide/200904/0904193279.html 但我不知道如何以及在何处将此代码放在我的StringGrid后代类中,以便在设计时/运行时更改RowCount时调用它
修改
在阅读你的评论后,我尝试了你的想法(似乎有效)来覆盖SizeChanged(我不知道那个方法存在,当我之前的serached时必须跳过它)。 无论如何,我把这段代码添加到了我的班级:
TMyGrid = class(TStringGrid)
private
...
procedure SizeChanged(OldColCount, OldRowCount: Longint); override;
procedure UpdateGridDimensions(NewColCount, NewRowCount: Integer);
...
procedure TMyGrid.SizeChanged(OldColCount, OldRowCount: Longint);
begin
inherited;
if (OldRowCount<>FRowCount)or(OldColCount<>ColCount) then
UpdateGridDimensions(ColCount, FRowCount);
end;
procedure TMyGrid.UpdateGridDimensions(NewColCount, NewRowCount: Integer);
var
C, R: Integer;
Old: Integer;
begin
if NewColCount <> ColCount then
begin
if NewColCount < ColCount then
begin
for R := 0 to RowCount-1 do
begin
for C := ColCount-1 downto NewColCount do
Objects[C, R].Free;
end;
end;
Old := ColCount;
ColCount := NewColCount;
if NewColCount > Old then
begin
for R := 0 to RowCount-1 do
begin
for C := Old to NewColCount-1 do
Objects[C, R] := TMyCell.Create;
end;
end;
end;
if NewRowCount <> RowCount then
begin
if NewRowCount < RowCount then
begin
for C := 0 to ColCount-1 do
begin
for R := RowCount-1 downto NewRowCount do
Objects[C, R].Free;
end;
end;
Old := RowCount;
RowCount := NewColCount;
if NewRowCount > Old then
begin
for C := 0 to ColCount-1 do
begin
for R := Old to NewRowCount-1 do
Objects[C, R] := TMyCell.Create;
end;
end;
end;
end;
但现在每当我放弃对表单的控制时,rowcount为93 ...我在哪里设置rowCount?因为我不喜欢。
然而,如果我将RowCount从93增加到其他类似100,那么我的对象存在前93行,但它们不会为93-100行创建...
所以这个想法听起来很棒,但它没有像我期望的那样起作用......
有什么想法? 我做错了吗?
答案 0 :(得分:0)
// SizeChanged - Called when the size of the grid has changed.
protected
procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
您可以覆盖动态方法SizeChanged并根据新大小初始化网格。您可以检查是否为设计时间(LU RD suggested链接)。正如David所提到的,最好为组件的使用者保留Objects属性。改为创建并使用自己的TList / TObjectList。