1

我有一个TStringGrid带有 2 列和 1 行 ( Property: ColCount = 2 & Rowcount = 1.

事件代码OnDrawCell

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
  var
    Parametertext : string;
begin
     case ACol of
     0 : Parametertext := 'Test';
     1 : Parametertext := 'Test1';
     end;
     stringgrid1.Brush.Color := clBtnFace;
     stringgrid1.Font.Color := clWindowText;
     stringgrid1.Canvas.FillRect(Rect);
     DrawText(stringgrid1.Canvas.Handle, PChar(parameterText), -1, Rect,
      DT_SINGLELINE);
end;

当我运行应用程序时,我得到以下输出: 样本输出

问题:

当我尝试使用StringGrid1.Cells[0,0], StringGrid1.Cells[1,0],获取文本时

我除了“Test”和“Test1”,但它总是给出一个空字符串“”。

如何使用从字符串网格中获取文本StringGrid.Cells[aCol,aRow]

4

2 回答 2

3

您正在生成文本以绘制它,但不存储它。您还需要设置 stringGrid.Cells 值,但可能不在 OnDrawCell 事件中。

想想你的变量Parametertext。它是退出时销毁的局部变量。您无法将其保存在其他任何地方。那么为什么你会期望它神奇地出现在单元格属性中呢?

于 2017-02-22T15:47:38.317 回答
0

要执行您的要求,您需要将字符串值实际存储在属性中,而不是在事件Cells中动态生成它们:OnDrawCell

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Parametertext : string;
begin
  Parametertext := StringGrid1.Cells[ACol, ARow];
  StringGrid1.Brush.Color := clBtnFace;
  StringGrid1.Font.Color := clWindowText;
  StringGrid1.Canvas.FillRect(Rect);
  DrawText(StringGrid1.Canvas.Handle, PChar(ParameterText), Length(ParameterText), Rect, DT_SINGLELINE);
end;

...

StringGrid1.Cells[0, 0] := 'Test';
StringGrid1.Cells[1, 0] := 'Test1';

如果您不打算使用该Cells属性来存储字符串,那么您还不如直接使用TDrawGrid

于 2017-02-22T20:15:43.737 回答