这是一个包含TStringGrid
组件的表单,其中包含2个TStringColumn
列,一个按钮和一个编辑框。单击该按钮应更改一个单元格的内容及其背景颜色,列的颜色和编辑框的颜色。单元格文本已正确更新和格式化,编辑框的颜色可以很好地改变,但单元格的颜色不会改变,列的颜色也不会改变。
我相信我已经遵循了Ray Konopka和Mike Sutton之前的所有提示...... 谁能告诉我为什么这部分代码不起作用?
我知道代码可能无法使用更高版本的FireMonkey进行编译。
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Grid, FMX.Layouts, FMX.Objects,
FMX.Edit;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
StringGrid1: TStringGrid;
StringColumn1: TStringColumn;
StringColumn2: TStringColumn;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
var
rect: TRectangle;
cell: TTextCell;
text: string;
begin
// Select a cell in column 1
cell:= stringColumn1.CellControlByRow(3) as TTextCell;
// Enter some text in cell
text:= 'hello sailor';
cell.TextAlign:= TTextAlign.taCenter;
cell.Text:= text; // works fine
// Change cell background color
rect:= cell.FindStyleResource('background') as TRectangle;
if rect <> nil then
rect.Fill.Color:= claRed; // does nothing
// Change color of all cells in grid
rect:= stringGrid1.FindStyleResource('background') as TRectangle;
if rect <> nil then
rect.Fill.Color:= claBisque; // works fine
// Change color of all cells in column 2
rect:= stringColumn2.FindStyleResource('background') as TRectangle;
if rect <> nil then
rect.Fill.Color:= claGreen; // does nothing
// Change background color of edit box
rect:= edit1.FindStyleResource('background') as TRectangle;
if rect <> nil then
rect.Fill.Color:= claBlue; // works fine
end;
end.