我一直在試圖學習如何與TGrid做到這一點,並感謝邁克薩頓的幫助已經處理了這一點。
[見Changing TTextCell background colour at runtime XE4背景]
已經成功地得到這個權利,我嘗試在TStringGrid類似的邏輯和它工作得很好。由於stringgrid不使用Grid1GetValue,我只是將隨機數硬編碼到FormCreate中的網格中。
[在下面的代碼中,我有一種叫做textcellstyle的樣式;到textcellstyle的「背景」組件,我添加了一個TRectangle,所以我可以調用TRectangle的'Fill'屬性。還沒上的風格頂部的是,看到以上鍊接]
我的代碼,如果它是有幫助的。
Procedure TForm1.FormCreate(Sender : TObject);
begin
{ CREATE AN EXTRA COLUMN }
StringGrid1.AddObject(TFinancialColumn.CreateStringGrid1));
{ HARD-CODE THE ROWS }
StringGrid1.Cells[0,0] :='0';
StringGrid1.Cells[0,1] :='1';
StringGrid1.Cells[0,2] :='2';
StringGrid1.Cells[0,3] :='3';
StringGrid1.Cells[0,4] :='4';
StringGrid1.Cells[0,5] :='5';
StringGrid1.Cells[0,6] :='6';
StringGrid1.Cells[0,7] :='7';
StringGrid1.Cells[0,8] :='8';
StringGrid1.Cells[0,9] :='9';
StringGrid1.Cells[0,10]:='10';
{ HARD-CODE A BUNCH OF NUMBERS. NOTE THAT HASH IN FRONT OF A NUMBER IS SIMPLY A FLAG FOR IsImportant }
StringGrid1.Cells[1,0] :='-10';
StringGrid1.Cells[1,1] :='-6.86999999';
StringGrid1.Cells[1,2] :='76.0999999';
StringGrid1.Cells[1,3] :='#10.25';
StringGrid1.Cells[1,4] :='#17.2900006';
StringGrid1.Cells[1,5] :='#57.1599993';
StringGrid1.Cells[1,6] :='21.86000';
StringGrid1.Cells[1,7] :='6.17';
StringGrid1.Cells[1,8] :='27.219999';
StringGrid1.Cells[1,9] :='#32.56000';
StringGrid1.Cells[1,10]:='-1.7999';
end;
Function TFinancialColumn.CreateCellControl : TStyledControl;
begin
Result:=TFinancialCell.Create(Self);
TTextCell(Result).OnTyping:=DoTextChanged;
TTextCell(Result).OnExit :=DoTextExit;
end;
Constructor TFinancialCell.Create(AOwner : TComponent);
begin
inherited;
StyleLookup:='textcellstyle';
StyledSettings:=StyledSettings-[TStyledSetting.ssStyle,TStyledSetting.ssFontColor]; { THIS LINE MUST BE HERE TO APPLY A NEW STYLE; IT CLEARS THE 'DEFAULT' STYLE SETTINGS }
TextAlign:=TTextAlign.taTrailing;
end;
Procedure TFinancialCell.SetData(const Value : TValue);
var
F : Single;
O : TFMXObject;
S : String;
begin
S:=Value.AsString;
If Length(S)>1 then
begin
FIsImportant:=S[1]='#';
If IsImportant then
S:=Copy(Value.AsString,2,MaxInt)
else
S:=Value.AsString;
F:=StrToFloat(S);
inherited SetData(Format('%n',[F]));
FIsNegative:=F<0;
ApplyStyling;
end;
end;
Procedure TFinancialCell.ApplyStyle;
var
T : TFMXObject;
begin
inherited;
T:=FindStyleResource('rectangle1');
If T is TRectangle then
begin
If IsNegative then
begin
TRectangle(T).Fill.Color:=claRed;
end;
end;
ApplyStyling;
end;
Procedure TFinancialCell.ApplyStyling;
var
T : TFMXObject;
begin
If IsNegative then
FontColor:=claBlack
else
FontColor:=claGreen;
If IsImportant then Font.Style:=[TFontStyle.fsItalic,TFontStyle.fsBold]; { REPEAT THE ITALIC ELSE IT WILL ONLY BE BOLD, IE IT OVERWRITES THE ITALIC WITH BOLD }
If Assigned(Font.OnChanged) then
Font.OnChanged(Font);
Repaint;
end;
此代碼工作再整中的字體和背景的條款。任何建議,以改善上述是非常受歡迎的,但希望這可以幫助。
儘管開始滾動時,樣式會崩潰,但還沒有弄清楚如何解決這個問題。任何建議將是最受歡迎的!
使用TGrid很容易。有了TStringGrid,它要麼不可能要麼很困難(我還沒有確定)。 – 2013-05-14 21:36:06