Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-07

Colouring StringGrids

9 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

Hi,

I'm using the following code to (unsuccessfully) colour the fields of a row in a tstringgrid if the second column is empty.

      if StringGrid2.Cells[1,i]='' then
      begin
        PriceHeader:=true;
        StringGrid2.Repaint;
      end;
procedure TForm1.StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var oldColour:TColor;
  oldBrush: TBrush;
begin
  with StringGrid2 do
  begin
    if PriceHeader then
    begin
      oldcolour:=font.Color;
      oldBrush:=Canvas.Brush;
      Brush.Color:=clNavy;
      font.Color:=clWhite;
      Canvas.FillRect(aRect);
      font.Color:=oldColour;
      Canvas.Brush:=oldBrush;
      PriceHeader:=false;
    end;
  end;
end;

Can anyone give me a clue as to why it's not working?

Reply by email

Héctor Fiandor Rosario
to Dave Coventry: I'm using the following code to (unsuccessfully) colour the fields of a row in a…

Dear Mr. Dave:

I am a new one in FP but I have some years in Delphi.

for coloring stringgrid?cells I use the following routine:

procedure TfBasica829.StringGrid2PrepareCanvas(sender: TObject; aCol,

  aRow: Integer; aState: TGridDrawState);

var

  aTextStyle: TTextStyle;

begin
{
clRed, clGreen, clYellow, clBlue,
clWhite, clGray, clFuchsia, clTeal,
clNavy, clMaroon, clLime, clOlive,
clPurple, clSilver, clAqua, clBlack
}

     if (ACol=0) then       //1
     begin
          Stringgrid2.Canvas.Brush.Color := clRed; //
          aTextStyle := StringGrid2.Canvas.TextStyle;
          aTextStyle.Alignment:=taCenter; //taRightJustify;
          aTextStyle.Layout:=tlCenter; //tlBottom;
          StringGrid2.Canvas.TextStyle := aTextStyle;
     end
     else
     if ACol=1 then       //1.5
     begin
          Stringgrid2.Canvas.Brush.Color := clOlive{Green}; //
          aTextStyle := StringGrid2.Canvas.TextStyle;
          aTextStyle.Alignment:=taCenter; //taRightJustify;
          aTextStyle.Layout:=tlCenter; //tlBottom;
          StringGrid2.Canvas.TextStyle := aTextStyle;
     end
     else
     if ACol=2 then       //2
     begin
          Stringgrid2.Canvas.Brush.Color := clYellow; //
          aTextStyle := StringGrid2.Canvas.TextStyle;
          aTextStyle.Alignment:=taCenter; //taRightJustify;
          aTextStyle.Layout:=tlCenter; //tlBottom;
          StringGrid2.Canvas.TextStyle := aTextStyle;
     end
     else
     if ACol=3 then       //2.5
     begin
          Stringgrid2.Canvas.Brush.Color := clAqua; //
          aTextStyle := StringGrid2.Canvas.TextStyle;
          aTextStyle.Alignment:=taCenter; //taRightJustify;
          aTextStyle.Layout:=tlCenter; //tlBottom;
          StringGrid2.Canvas.TextStyle := aTextStyle;
     end
     else
     if ACol=4 then       //3.0
     begin
          Stringgrid2.Canvas.Brush.Color := clSilver; //
          aTextStyle := StringGrid2.Canvas.TextStyle;
          aTextStyle.Alignment:=taCenter; //taRightJustify;
          aTextStyle.Layout:=tlCenter; //tlBottom;
          StringGrid2.Canvas.TextStyle := aTextStyle;
     end;
end;

to obtain this

maybe this can help you.

bye,

Ing. H?ctor F. Fiandor

Earlier message 62 lines
----- Original Message ----- From: "Dave Coventry" To: "General mailing list" Sent: Tuesday, July 15, 2008 7:15 AM Subject: [Lazarus] Colouring StringGrids > Hi, > > I'm using the following code to (unsuccessfully) colour the fields of > a row in a tstringgrid if the second column is empty. > > if StringGrid2.Cells[1,i]='' then > begin > PriceHeader:=true; > StringGrid2.Repaint; > end; > > > procedure TForm1.StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer; > aRect: TRect; aState: TGridDrawState); > var oldColour:TColor; > oldBrush: TBrush; > begin > with StringGrid2 do > begin > if PriceHeader then > begin > oldcolour:=font.Color; > oldBrush:=Canvas.Brush; > Brush.Color:=clNavy; > font.Color:=clWhite; > Canvas.FillRect(aRect); > font.Color:=oldColour; > Canvas.Brush:=oldBrush; > PriceHeader:=false; > end; > end; > end; > > Can anyone give me a clue as to why it's not working? > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus > > __________ Informaci?n de ESET Smart Security, versi?n de la base de firmas de virus 3268 (20080715) __________ > > ESET Smart Security ha comprobado este mensaje. > http://www.eset.com > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20080715/7fcec315/attachment-0001.html -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/jpeg Size: 17209 bytes Desc: not available Url : http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20080715/7fcec315/attachment-0001.jpe

Reply by email

Dave Coventry
to Graeme Geldenhuys: Have you tried setting DefaultDrawing = False? - Graeme -…

Hi Graeme,

It's quite interesting when set the DefaultDrawing to false: the grid isn't quite what I'm after...

2008/7/15 Graeme Geldenhuys :

Show quoted text 19 lines
On Tue, Jul 15, 2008 at 1:15 PM, Dave Coventry wrote: > > Can anyone give me a clue as to why it's not working? Have you tried setting DefaultDrawing = False? Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Michael Van Canneyt
to Dave Coventry: I'm using the following code to (unsuccessfully) colour the fields of a row in a…
Dave Coventry wrote 10 lines
Hi, I'm using the following code to (unsuccessfully) colour the fields of a row in a tstringgrid if the second column is empty. if StringGrid2.Cells[1,i]='' then begin PriceHeader:=true; StringGrid2.Repaint; end;

This kind of logic should only be used in the PrepareCanvas or DrawCall events.

Show quoted text 7 lines
procedure TForm1.StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState); var oldColour:TColor; oldBrush: TBrush; begin

I would insert here

  PriceHeader:=(ACol=1) and (ARow=Something) and (Cells[ACol,Something]='');

And make PriceHeader a local variable.

Michael.

Reply by email

Coppola Ing. Salvatore
to Dave Coventry: Thanks for the help. I've tried the code as you suggest, but it only seems to affect the…
Dave Coventry wrote 14 lines
Hi Hector, Thanks for the help. I've tried the code as you suggest, but it only seems to affect the selected row. I'm having difficulty following the logic process. _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus
Maybe this can help yuo:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
begin
if (aCol=aRow) then begin
StringGrid1.Canvas.Brush.Color:=clYellow;
StringGrid1.Canvas.Rectangle(StringGrid1.CellRect(aCol,aRow));
end;
end;

Reply by email

Coppola Ing. Salvatore
Dave Coventry wrote 14 lines
Hi Hector, Thanks for the help. I've tried the code as you suggest, but it only seems to affect the selected row. I'm having difficulty following the logic process. _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus
Maybe this can help yuo:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
begin
  if (aCol=aRow) then begin
    StringGrid1.Canvas.Brush.Color:=clYellow;
    StringGrid1.Canvas.Rectangle(StringGrid1.CellRect(aCol,aRow));
  end;
end;

Regards,
Salvatore

Reply by email