我正在运行Lazarus 0.9.30。如何将对象与TGridColumns对象关联
我有一个窗体上的标准TStringGrid,并有一个函数动态地添加TGridColumns对象在它运行时。我有一个包含每列(我在运行时从文件中读出)的所有属性的对象集合,并且我想将每个对象与其相应的列标题关联起来。
我试过下面的代码,但在运行时,当我试图访问列标题对象后面的对象时,我得到一个'无返回的对象。我怀疑这是发生的原因是网格单元格(保存列标题)是空白的,并且您不能将对象与空的网格单元关联。
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;
您好,1)你能不能添加'TTmColumnTitle'和'TTmColumnTitles'的声明,请2)你肯定有你在哪里存储对象,以最高的索引行? – TLama 2012-02-26 21:17:07
我从你的评论中解决了这个问题。你是对的,我用索引来分配对象到行中的列。我已经使用可用的示例更新了代码示例。 – user1174918 2012-02-27 10:32:59