刷新 Delphi StringGrid

huangapple go评论56阅读模式
英文:

Delphi StringGrid refresh

问题

Learning Delphi (have a ways to go), using Rio.

I figured out how to use a colored background in TStringGrid rows - but it looks like I need to refresh when data in those rows changes (so as to get different colors to show up based on the data changes).

I thought that just setting the cell values to their then-existing values would cause the refresh. But it didn't. I could tell for sure that it didn't - because I had a debug breakpoint placed within the StringGrid1DrawCell procedure - and that breakpoint was not hit.

The code that I had been using to hopefully cause the refresh in TStringGrid was as follows (note: S is defined as a String):

S := StringGrid1.Cells[1, i];
StringGrid1.Cells[1, i] := S;

Is the basic assumption (that just setting/resetting values of the cell contents causes a refresh) in error?

If the idea is right, but the method is wrong: could you let me know what to do differently?

英文:

Learning Delphi (have a ways to go), using Rio.

I figured out how to use a colored background in TStringGrid rows - but it looks like I need to refresh when data in those rows changes (so as to get different colors to show up based on the data changes).

I thought that just setting the cell values to their then-existing values would cause the refresh. But it didn't. I could tell for sure that it didn't - because I had a debug breakpoint placed within the StringGrid1DrawCell procedure - and that breakpoint was not hit.

The code that I had been using to hopefully cause the refresh in TStringGrid was as follows (note: S is defined as a String):

S := StringGrid1.Cells[1, i];
StringGrid1.Cells[1, i] := S;

Is the basic assumption (that just setting/resetting values of the cell contents causes a refresh) in error?

If the idea is right, but the method is wrong: could you let me know what to do differently?

答案1

得分: 3

OnDrawCell 事件仅在需要在屏幕上绘制特定单元格时触发。

设置特定单元格的值将仅使该单元格失效,从而触发仅对该单元格进行重绘,而不是整行或整个网格。

如果需要触发整行的重绘,请调用网格的(受保护的)InvalidateRow() 方法,例如:

type
  TStringGridAccess = class(TStringGrid)
  end;

procedure TMyForm.DoSomething;
begin
  ... 
  StringGrid1.Cells[1, i] := ...; 
  TStringGridAccess(StringGrid1).InvalidateRow(i);
  ...
end;

如果需要触发整个网格的重绘,请调用网格的(公共的)Invalidate() 方法,例如:

StringGrid1.Cells[1, i] := ...;
StringGrid1.Invalidate;
英文:

The OnDrawCell event is fired only when a given cell needs to be painted onscreen.

Setting a specific cell's value will invalidate only that cell, thus triggering a repaint of only that cell, not the cell's entire row, or the grid as a whole.

If you need to trigger a repaint of an entire row, call the grid's (protected) InvalidateRow() method, eg:

type
  TStringGridAccess = class(TStringGrid)
  end;

procedure TMyForm.DoSomething;
begin
  ... 
  StringGrid1.Cells[1, i] := ...; 
  TStringGridAccess(StringGrid1).InvalidateRow(i);
  ...
end;

If you need to trigger a repaint of the entire grid, call the grid's (public) Invalidate() method, eg:

StringGrid1.Cells[1, i] := ...;
StringGrid1.Invalidate;

huangapple
  • 本文由 发表于 2023年2月18日 07:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定