2

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

这个问题是我上一个问题的延伸

上一个问题围绕如何更改我在运行时加载到标准 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 事件的行为以及为什么我会看到这个吗?

4

1 回答 1

2

正如您所怀疑的那样,ClearRowCountand设置为 0。ColCount那么它也被清除是很合乎逻辑的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;
于 2012-03-12T10:02:33.543 回答