### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Colore celle StringGrid  (Read 432 times)

#### JuanBell

• New member
• Posts: 8
##### Colore celle StringGrid
« on: April 24, 2024, 08:47:28 pm »
Salve a tutti
per favore mi dite come so colora una specifica cella di StringGrid con un colore data una condizione.
Grazie dell’attenzione.
for J:=1 to 10
do begin
L:=L+1;
LS:=IntToStr(L);
If not Odd(I) Then CC:='Z' e colora cella in verde
else CC:='A'; e colora cella in rosso
StringGrid1.Cells[I,J]:=CC;
end;
end

#### qk

• New Member
• Posts: 33
##### Re: Colore celle StringGrid
« Reply #1 on: April 24, 2024, 09:34:32 pm »
Se non hai dimistichezza con l'inglese aggiungi comunque una copia
In questo modo il messaggio sarà comprensibile a tutti e più persone potranno aiutarti.

Sotto un semplice esempio di come colorare le colonne dispari.

If you don't speak English well, still add a copy of your message
translated with Google translator. This way, the message will be

Below is a simple example of how to color the odd columns.

Code: Pascal  [Select][+][-]
1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
2.   aRect: TRect; aState: TGridDrawState);
3. begin
4.   if (ACol > 0) and (ARow > 0) then
5.     with Sender as TStringGrid do
6.     begin
7.       if Odd(ACol) then
8.       begin
9.         Canvas.Brush.Color := clGreen;
10.         Canvas.FillRect(aRect);
11.         Canvas.TextOut(aRect.Left + 2, ARect.Top + 2, Cells[ACol, ARow]);
12.       end else
13.       begin
14.         Canvas.Brush.Color := clRed;
15.         Canvas.FillRect(aRect);
16.         Canvas.TextOut(aRect.Left + 2, ARect.Top + 2, Cells[ACol, ARow]);
17.       end;
18.     end;
19. end;

#### Lutz Mändle

• Jr. Member
• Posts: 67
##### Re: Colore celle StringGrid
« Reply #2 on: April 24, 2024, 09:45:10 pm »
Use the OnPrepareCanvas event of the stringgrid for coloring the cells, for example like this:

Code: Pascal  [Select][+][-]
1. procedure TForm1.StringGrid1PrepareCanvas(Sender: TObject; aCol,
2.   aRow: Integer; aState: TGridDrawState);
3. begin
4.   if (ACol > 0) and (ARow > 0) then
5.     if Odd(aCol) then
6.       TStringGrid(Sender).Canvas.Brush.Color := clGreen
7.     else
8.       TStringGrid(Sender).Canvas.Brush.Color := clRed;
9. end;
10.

#### JuanBell

• New member
• Posts: 8
##### Re: Colore celle StringGrid
« Reply #3 on: April 24, 2024, 10:37:56 pm »
Grazie di tutto thanks for everything