Recent

Author Topic: TStringGrid AlternateColor  (Read 526 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
TStringGrid AlternateColor
« on: March 24, 2025, 12:12:13 pm »
TStringGrid has two properties: StringGrid.Color and StringGrid.AlternateColor.  Is there a property for how many rows in each alteration?

Example:

A setting of 1 would do: (This is how it behaves now.)
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10

A setting of 2 would do:
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8

Row 9
Row 10

A setting of 3 would do:
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6

Row 7
Row 8
Row 9
Row 10
Row 11
Row 12


Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: TStringGrid AlternateColor
« Reply #1 on: March 24, 2025, 12:33:42 pm »
you mean kinda like "3 rows in black, 4 rows in red, 3 rows in black....."?

Not that i know of....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

bytebites

  • Hero Member
  • *****
  • Posts: 708
Re: TStringGrid AlternateColor
« Reply #2 on: March 24, 2025, 12:49:42 pm »
Code: Pascal  [Select][+][-]
  1. procedure TJunailija.listPrepareCanvas(Sender: TObject; aCol, aRow: integer;
  2.   aState: TGridDrawState);
  3. begin
  4.    if arow>0 then TStringGrid(Sender).Canvas.Font.color := if (arow-1) div 3 mod 2=0 then clRed else clBlack;
  5. end;
  6.  

wp

  • Hero Member
  • *****
  • Posts: 12752
Re: TStringGrid AlternateColor
« Reply #3 on: March 24, 2025, 12:57:04 pm »
The AlternateColor mechanism is valid only for every 2nd row. If you want a different behaviour you can handle the OnPrepareCanvas event which, among others, gets the row index as parameter, and you can change the Canvas.Brush.Color as you need.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1PrepareCanvas(Sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. const
  4.   BLOCK_SIZE = 3;   // count of rows having the same color
  5. var
  6.   grid: TStringGrid;
  7. begin
  8.   grid := TStringGrid(Sender);
  9.   if (ARow >= grid.FixedRows) and (ACol >= grid.FixedCols) then  // avoid re-coloring the fixed cells
  10.   begin
  11.     if odd((ARow - grid.FixedRows) div BLOCK_SIZE) then
  12.       grid.Canvas.Brush.Color := clWindow
  13.     else
  14.       grid.Canvas.Brush.Color := grid.AlternateColor;
  15.   end;
  16. end;
  17.  

Nicole

  • Hero Member
  • *****
  • Posts: 1066
Re: TStringGrid AlternateColor
« Reply #4 on: March 26, 2025, 04:46:45 pm »
Here is a code snipet, which does a lot of coloring. Not all Vars will make sense to your software. Just delete or replace them. This is an example of various colouring of lines and rows of a DBBrid. As the coloring is used conditional to the content, I used this like as Calc-event at the same time.

Code: Pascal  [Select][+][-]
  1.  // malt die Monateszeilen der Trades je nach Monat bunt und rechnet sie auch, als "Calc"-Ereignis missbraucht
  2.  procedure TFrame_Konto.DBGrid_TradesDrawColumnCell(Sender: TObject;
  3.    const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  4.  
  5.  Var s_cell: string;
  6.      d: double;
  7.      i: integer;
  8.      dat_: TDateTime;
  9.      monat_, woche_: integer;
  10.  
  11.  begin
  12.  // wird jede Zelle aufgerufen, dabei liegen dieParameter so:
  13.  ///  IBQuery_Alltag.RecNo 1 1 1 1      => DataSource_TradesListen.DataSet.RecNo
  14.  ///                       2 2 2 2
  15.  ///  column.index 0 1 2 3
  16.  ///        0 1 2 3
  17.  ///        0 1 2 3
  18.  ///  columns 0 1 2 3
  19.  ///         0 1 2 3
  20.  ///        0 1 2 3
  21.  ///  hier passiert nur das ZEICHNEN, der Inhalt hängt davon nicht ab
  22.  ///  ACHTUNG Namen der Felder sind case sensitive!
  23.  
  24.   with (Sender as TDBGrid) do begin
  25.  
  26.      if Column.Field <> nil then s_cell:=Column.Field.AsString; // hier wird einmal alles generell in ein eintragbares String gespeichert
  27.                // ganz unten wird dieser String über den neuen Hintergrund geschrieben
  28.                // für andere Werte überschreibe ich das String, um es anders zu formatieren.
  29.    //________________ diese Block färbt alternierende Zeilen, wenn NICHT nach Datum sortiert wird
  30.     if (RadioGroup_Sortiere.ItemIndex <> 5) and (RadioGroup_sortieren2.ItemIndex <> 0) then begin
  31.       i:=DataSource_TradesListen.DataSet.RecNo;
  32.       if Odd(i) then Canvas.Brush.Color :=$00E7F2FF;
  33.       Canvas.FillRect(Rect);  // steht das vor dem Block "Schriftfarbe", wird nur hinter-dem-Anfüllen-aufgerufen sichtbar
  34.       Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,s_cell);  // Text wird neu geschrieben
  35.       exit;  // Sortierung der Query UNGLEICH Datum, daher macht das Einfärben oder Rechnen keinen Sinn
  36.     end;
  37.  
  38.     //__________alles weitere bis hinunter, WENN nach Datum sortiert wird
  39.     // die Spalte Datum wird ausgelesen und für diese Zeile verarbeitet
  40.      dat_:=DataSource_TradesListen.DataSet.FindField('Entry_Date').AsDateTime;
  41.      monat_ := MonthOf(dat_);
  42.      woche_:= WeekOf(dat_);
  43.  
  44.    // _________________jedes neue Monat wird erkannt
  45.     if monat_ <> monat_temp then begin
  46.        monat_temp:=monat_;
  47.        Monatswechsel:=true;
  48.        Monatsgewinn:=0;
  49.     end;
  50.  
  51.    if Monatswechsel // and (DataSource_TradesListen.DataSet.RecNo > 1)
  52.    then begin   // das zeichnet den oberen Rand einer einzelnen Zelle, außer ganz oben
  53.        Canvas.Pen.Color := $00243C79; // $005180F5; // Linienfarbe für die Monatstrennung
  54.        Canvas.Pen.Width := 5; // Stärke der Linie, erzeugt einen Ballon am Zeilenbeginn
  55.        Canvas.Pen.Style := psSolid;
  56.        Canvas.MoveTo(Rect.Left,Rect.Top);
  57.        Canvas.LineTo(Rect.Right,Rect.Top); // zeichnet eine rote Linie an die Oberkante und damit unter das alte Monat
  58.          end;
  59.  
  60.     if (DataCol > 12)   // das funktioniert jetzt, doch nur für > 12?!  Leider ja, obwohl 28 Spalten da, wird nur bis 13 gezählt.
  61.          then Monatswechsel:=false;   // Wegen eines internen Fehlers in der Query wird DataCol und FieldCount und ColumnCourt leider
  62.             // nicht nur fehelrhaft gezählt, es kommt auch zu Zugriffen nach nil.   17.8.2023 unklar, ob das in Delphi so war oder Lazarus, eher Delphi
  63.  
  64.     if woche_ <> woche_temp then begin
  65.        woche_temp:=woche_;
  66.        If ListBoxZeilenFarbe = clwhite then ListBoxZeilenFarbe := ListBoxZeilenFarbe // wechselt mit der als neu erkannten Woche
  67.                                        else ListBoxZeilenFarbe := clWhite;
  68.        Wochengewinn:=0; // Der Wochengeweinn wird zurückgeetzt
  69.     end;
  70.     Canvas.Brush.Color := ListBoxZeilenFarbe; // Hintergrundfarbe wird zugewiesen, bewirkt, dass die Hingrundfarbe orange bleibt, bis zum nächsten Wochenwechsel
  71.    //_____________ Ende der Wochenbearbeitung
  72.  
  73.  
  74.      // überschreibt string für die PL wieder, um die PL besser zu formatieren, die interne Breite bleibt leider bei der vollen Länge
  75.      if (Column.FieldName='PL') then begin // klappt auch: if (dataCol=1).....
  76.           d:=DataSource_TradesListen.DataSet.FindField('PL').AsFloat;
  77.           s_cell:=FloatToStrF(d,ffNumber,5,2);
  78.        end;
  79.  
  80.      if (DataCol = 1) then   // 1 ist willküflich, damit nicht jede Zelle wieder gerechnet wird
  81.         myKontostand:=DB_Konto.AddiereKapitalUGeschlossenPos; // liest nur den LETZTEN Kontostand, nicht jenen, des richtigen Datums, den speichere ich nicht
  82.  
  83.      // adddiert die Tradeergenisse zum Periodenfangskapital, rechnet Performance
  84.       if (Column.FieldName='ERGEBNIS') then begin        // das ist case sensitive !!!
  85.           i:=DataSource_TradesListen.DataSet.FindField('ERGEBNIS').AsInteger;
  86.           if i < 0 then Canvas.Font.Color := clRed
  87.                    else Canvas.Font.Color := clGreen;
  88.          //  myKontostand:= myKontostand + i;   18.8.2023 da ich die Kontostände nicht nach Datum speichere, habe ich die Zahl "Kontostand an einem Tag" nicht
  89.            Monatsgewinn:=Monatsgewinn + i;
  90.            Wochengewinn:=Wochengewinn + i;
  91.         end;
  92.  
  93.       if (Column.FieldName='KAPITAL') then     // schreibt die Var myKontostand ins Feld
  94.           s_cell:=FloatToStrF(myKontostand,ffNumber,5,2);
  95.  
  96.       if (Column.FieldName='Wochengewinn') then begin
  97.           s_cell:=FloatToStrF(Wochengewinn,ffNumber,5,2);
  98.           if Wochengewinn < 0 then Canvas.Font.Color := clRed
  99.                    else Canvas.Font.Color := clGreen;
  100.                                                 end;
  101.  
  102.       if (Column.FieldName='WoG_Prozent') then begin
  103.           if myKontostand = 0 then d:=0
  104.                               else d:=(Wochengewinn / myKontostand) * 100;
  105.           s_cell:=FloatToStrF(d,ffNumber,5,2) + ' %';
  106.           if d < 0 then Canvas.Font.Color := clRed
  107.                    else Canvas.Font.Color := clGreen;
  108.                                                 end;
  109.  
  110.       if (Column.FieldName='Monatsgewinn') then begin
  111.           s_cell:=FloatToStrF(Monatsgewinn,ffNumber,5,2);
  112.           if Monatsgewinn < 0 then Canvas.Font.Color := clRed
  113.                    else Canvas.Font.Color := clGreen;
  114.                                                 end;
  115.  
  116.       if (Column.FieldName='MoG_Prozent') then begin
  117.           if myKontostand = 0 then d:=0
  118.                               else d:=(Monatsgewinn / myKontostand) * 100;
  119.           s_cell:=FloatToStrF(d,ffNumber,5,2) + ' %';
  120.           if d < 0 then Canvas.Font.Color := clRed
  121.                    else Canvas.Font.Color := clGreen;
  122.                                                 end;
  123.  //__________________ Ende Performancerechnungen
  124.  
  125.  
  126.       if (gdSelected in State) then Canvas.Font.Color:= clBlue; // die angeklickte Zelle bekommt blaue Schrift
  127.       // 30.1.2023 jetzt ist sie weiß, doch uU nicht "selected
  128.       if (gdFocused in State) then Canvas.Font.Color:= clBlue; // probere es mal so und auch blau
  129.  
  130.       Canvas.FillRect(Rect);  // steht das vor dem Block "Schriftfarbe", wird nur hinter-dem-Anfüllen-aufgerufen sichtbar
  131.       Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,s_cell);  // Text wird neu geschrieben
  132.  
  133.    end;  // zu (Sender as TDBGrid)
  134.  end;  
                                           

 

TinyPortal © 2005-2018