【问题标题】:How do you associate an object to a TGridColumns object如何将对象关联到 TGridColumns 对象
【发布时间】:2012-02-27 18:47:05
【问题描述】:

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

【问题讨论】:

  • 您好,1) 您能否添加 TTmColumnTitleTTmColumnTitles 的声明,请 2) 您确定将对象存储到最高索引行的位置吗?跨度>
  • 我从您的评论中解决了这个问题。你是对的,我用来将对象分配给行内列的索引存在问题。我已经用一个有效的示例更新了代码示例。

标签: lazarus tstringgrid


【解决方案1】:

仅将对象分配给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;

【讨论】:

  • 我认为 OP 已经是 doing that,但是关于 draw 事件的观点是正确的。在我看来,对象也存储在错误的行中。
  • 有一个TStringGrid.Columns 属性,一个TGridColumn 的集合,其中包含Title.Caption,所以没有必要(甚至可能不起作用)使用TStringGrid.Cells 作为标题. Lazarus 的实现与 Delphi 的实现相差甚远。
  • 啊,对不起:拉撒路!傻我!
  • 你好@NGLN .....你是对的,将对象分配给行实际上不会显示 GridColumn.Title.Caption。我确实有一个执行此操作的 OnDrawCell 事件处理程序。您和 TLama 都正确地指出我的第二个“for”循环中的索引使用不正确。实际上,我最终将第二个“for”循环全部删除,并添加了代码行“aGrid.Objects[anIndex1, 0] := aColumnTitle;”由您自己建议 NGL 到代码示例中的第一个“for”循环(我已经更新了我的原始代码示例)。
  • 我已经删除了“误导性”Delphi 标签。
猜你喜欢
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
相关资源
最近更新 更多