【问题标题】:Why Do The Cell Properties In A TStringGrid Column Title Cells Change When A Grid Is Loaded?为什么加载网格时 TStringGrid 列标题单元格中的单元格属性会发生变化?
【发布时间】:2012-03-13 01:45:48
【问题描述】:

我正在运行 Lazarus v0.9.30(32 位编译器)。

这个问题是我之前的question 的延伸。

上一个问题围绕如何将我在运行时加载到标准 TStringGrid 的 TGridColumns 对象中的文本方向更改。该解决方案涉及覆盖字符串网格的 DrawCellText 事件。

我的问题是这样的。当我尝试加载 TStringGrid 时,我发现文本方向保持不变,但列单元格高度变回默认高度。

我用来加载网格的代码如下所示。

procedure TTmMainForm.vLoadWorldScoutGrid;
var
  aMember : TTmMember;
  anIndex1: integer;
  anIndex2: integer;
begin
  //Clear the string grid and set the row count to 1 to take into account the fixed row.
  SgWorldScout.Clear;
  SgWorldScout.RowCount := 1;

  for anIndex1 := 0 to Pred(FManager.Members.Count) do
  begin
    //Add a new row to the string grid.
    SgMembers.RowCount := SgMembers.RowCount + 1;

    //Get the TTmMember object from the collection.
    aMember := TTmMember(FManager.Members.Items[anIndex1]);

    //Populate the row cells in the string grid.
    SgMembers.Cells[0, SgMembers.RowCount - 1] := aMember.stMemberNumber;
    SgMembers.Cells[1, SgMembers.RowCount - 1] := aMember.stPatrol;
    SgMembers.Cells[2, SgMembers.RowCount - 1] := aMember.stSurname;
    SgMembers.Cells[3, SgMembers.RowCount - 1] := aMember.stFirstName;

    //Add the TTmMember object to every row cell.
    for anIndex2 := 0 to SgMembers.ColCount - 1 do
      SgMembers.Objects[anIndex2, SgMembers.RowCount - 1] := aMember;
  end; {for}}

  vSetWorldScoutGridPushbuttons;
end; 

我怀疑当我调用“SgWorldScout.Clear”时,字符串网格单元格的属性可能会在调用默认 DrawCellText 事件时被重置/修改,这将解释单元格高度的变化。不知道为什么文本方向也不会改变。有人能解释 DrawCellText 事件的行为以及为什么我会看到这个吗?

【问题讨论】:

  • 在这个过程中你到底想清楚什么?你真的要清除整个网格吗?有一组函数,TCustomStringGrid.Clean 用于仅清理网格特定单元格区域的单元格内容。
  • 是的,我只想清理数据行...而不是固定行。所以我想清除所有非固定单元格文本和任何指向关联对象的指针。
  • 那么答案就在下面 :) 你不需要清除整个网格。将行数设置为所需值是非常安全(并且常用)的,因此只需将 RowCount 设置为 1(不带 Clear)将保留您的标题(及其所有设置)。

标签: lazarus tstringgrid text-rendering


【解决方案1】:

ClearRowCountColCount 设置为 0,正如您所怀疑的那样。那么 RowHeights 也被清除是很合乎逻辑的,因为当您将 RowCount 设置为 0 时,就没有要存储的高度了。如果您想清除并仅添加非固定行,则只需将 RowCount 设置为 1 而无需清除整个网格。所以这样修改你的代码:

procedure TTmMainForm.vLoadWorldScoutGrid;
var
  aMember : TTmMember;
  anIndex1: integer;
  anIndex2: integer;
begin
  // set the row count to 1 to keep the fixed row along with its settings
  SgWorldScout.RowCount := 1;

  for anIndex1 := 0 to Pred(FManager.Members.Count) do
  ...
end;

【讨论】:

    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多