2012-02-26 92 views
2

我正在运行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

您好,1)你能不能添加'TTmColumnTitle'和'TTmColumnTitles'的声明,请2)你肯定有你在哪里存储对象,以最高的索引行? – TLama 2012-02-26 21:17:07

+0

我从你的评论中解决了这个问题。你是对的,我用索引来分配对象到行中的列。我已经使用可用的示例更新了代码示例。 – user1174918 2012-02-27 10:32:59

回答

2

仅将对象分配给Objects属性是不够的。您必须自己在OnDrawCell事件处理程序中从该对象中绘制标题标题,或者指定Cells属性。

,你不能是空

是的,你可以网格单元对象相关联。一个单元格的字符串和对象“工作”彼此独立。

所以它应该是:

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; 
+0

我认为OP已经['那样'](http://stackoverflow.com/a/9155357/960757),但是关于绘制事件的观点是正确的。在我看来,这些对象存储在错误的行中。 – TLama 2012-02-27 10:20:10

+0

有一个'TStringGrid.Columns'属性,它是一个'TGridColumn'的集合,它具有'Title.Caption',所以没有必要(甚至可能不工作)使用TStringGrid.Cells作为头部。拉法罗斯的实施与德尔福的实施相差甚远。 – TLama 2012-02-27 10:27:36

+1

啊,对不起:拉撒路!傻我! – NGLN 2012-02-27 10:28:22