我正在运行 Lazarus 0.9.30。
我的表单上有一个标准的 TStringGrid,并且有一个在运行时动态向其中添加 TGridColumns 对象的函数。我有一个对象集合,其中包含每列的所有属性(我在运行时从文件中读取),并且我希望将每个对象与其相应的列标题相关联。
我已经尝试了下面的代码,但在运行时,当我尝试访问列标题对象后面的对象时,我得到一个返回的“nil”对象。我怀疑发生这种情况的原因是网格单元格(保存列标题)为空,并且您无法将对象与空的网格单元格关联。
type
TTmColumnTitles = class(TTmCollection)
public
constructor Create;
destructor Destroy; override;
function stGetHint(anIndex : integer) : string;
end;
type
TTmColumnTitle = class(TTmObject)
private
FCaption : string;
FCellWidth : integer;
FCellHeight : integer;
FFontOrientation : integer;
FLayout : TTextLayout;
FAlignment : TAlignment;
FHint : string;
procedure vInitialise;
public
property stCaption : string read FCaption write FCaption;
property iCellWidth : integer read FCellWidth write FCellWidth;
property iCellHeight : integer read FCellHeight write FCellHeight;
property iFontOrientation : integer read FFontOrientation write FFontOrientation;
property Layout : TTextLayout read FLayout write FLayout;
property Alignment : TAlignment read FAlignment write FAlignment;
property stHint : string read FHint write FHint;
constructor Create;
destructor Destroy; override;
end;
procedure TTmMainForm.vLoadGridColumnTitles
(
aGrid : TStringGrid;
aCollection : TTmColumnTitles
);
var
GridColumn : TGridColumn;
aColumnTitle : TTmColumnTitle; //Just a pointer!
anIndex1 : integer;
anIndex2 : integer;
begin
for anIndex1 := 0 to aCollection.Count - 1 do
begin
aColumnTitle := TTmColumnTitle(aCollection.Items[anIndex1]);
GridColumn := aGrid.Columns.Add;
GridColumn.Width := aColumnTitle.iCellWidth;
GridColumn.Title.Font.Orientation := aColumnTitle.iFontOrientation;
GridColumn.Title.Layout := aColumnTitle.Layout;
GridColumn.Title.Alignment := aColumnTitle.Alignment;
GridColumn.Title.Caption := aColumnTitle.stCaption;
aGrid.RowHeights[0] := aColumnTitle.iCellHeight;
aGrid.Objects[anIndex1, 0] := aColumnTitle;
end; {for}
end;
最佳答案
仅将对象分配给 Objects
属性是不够的。您必须在 OnDrawCell
事件处理程序中自己从该对象绘制标题说明,或者也分配 Cells
属性。
and you can't associate objects with grid cells that are empty
是的,你可以。一个单元格的字符串和对象彼此独立地“工作”。
所以应该是:
for anIndex2 := 0 to aGrid.ColCount - 1 do
begin
aColumnTitle := aCollection.Items[anIndex2]; // Is aCollection.Count in sync
// with aGrid.ColCount??
aGrid.Cells[anIndex2, 0] := aColumnTitle.Caption;
aGrid.Objects[anIndex2, 0] := aColumnTitle;
end;
关于lazarus - 如何将对象关联到 TGridColumns 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450639/