Recent

Author Topic: [Solved] Stringgrid, checkboxcell and ondrawcell  (Read 2708 times)

madepabloh

  • Full Member
  • ***
  • Posts: 160
[Solved] Stringgrid, checkboxcell and ondrawcell
« on: October 20, 2014, 10:32:59 am »
Hi all,

I am trying to change the color of the columns of an stringgrid by using a toolbutton to colorize or not that cells. I made it writing this code:
Code: [Select]
if (ARow > 0) then
        begin
          if aCol < 1 then FilesGrid.Canvas.Brush.Color := clWhite
          else if aCol <6 then FilesGrid.Canvas.Brush.Color := clMoneyGreen
          else if aCol <8 then FilesGrid.Canvas.Brush.Color := clWhite
          else if aCol < 17 then FilesGrid.Canvas.Brush.Color := $00E1FFF9    //Pale yellow
          else if aCol < 26 then FilesGrid.Canvas.Brush.Color := $00FFEBDF    //Pale blue
          else if aCol < 29 then FilesGrid.Canvas.Brush.Color := $00D3FFE4    //Pale green
          else if aCol < 33 then FilesGrid.Canvas.Brush.Color := $00FFE5FC;   //Pale pink
          FilesGrid.Canvas.FillRect(aRect);
          FilesGrid.Canvas.Font.Color := clBlack;
          FilesGrid.canvas.textrect(aRect, aRect.Left + 4, aRect.Top, FilesGrid.cells[acol,arow])
        end; 

Although it works nice, the problem is that many of those columns contains a checkbox. Then, when the button is used to colorize the cell, the checkbox dissapear to show 1 or 0. When the stringgrid is in edit mode, i am able to change from 1 to 0 or viceversa by clicking in the cell. So, it works, but it does not show the checkbox.

Then, my questions are if this is a bug or if i must complete my code to say to lazarus to paint again the checkboxes and how to do it.

Thanks so much!
« Last Edit: October 20, 2014, 12:08:22 pm by madepabloh »

wp

  • Hero Member
  • *****
  • Posts: 11911
Re: Stringgrid, checkboxcell and ondrawcell
« Reply #1 on: October 20, 2014, 10:40:09 am »
Unfortunately you do not show which method you use to colorize the columns, I guess it's OnDrawCell. In this method you have to do everything by yourself. OnPrepareCanvas is a better choice, you just have to set the canvas properties, and the grid will paint everything with these properties.

Code: [Select]
procedure TForm1.GridPrepareCanvas(Sender: TObject);
begin
  if (ARow > 0) then begin
    if aCol < 1 then FilesGrid.Canvas.Brush.Color := clWhite
    else if aCol <6 then FilesGrid.Canvas.Brush.Color := clMoneyGreen
    else if aCol <8 then FilesGrid.Canvas.Brush.Color := clWhite
    else if aCol < 17 then FilesGrid.Canvas.Brush.Color := $00E1FFF9    //Pale yellow
    else if aCol < 26 then FilesGrid.Canvas.Brush.Color := $00FFEBDF    //Pale blue
    else if aCol < 29 then FilesGrid.Canvas.Brush.Color := $00D3FFE4    //Pale green
    else if aCol < 33 then FilesGrid.Canvas.Brush.Color := $00FFE5FC;   //Pale pink
//          FilesGrid.Canvas.FillRect(aRect);
//          FilesGrid.Canvas.Font.Color := clBlack;
//          FilesGrid.canvas.textrect(aRect, aRect.Left + 4, aRect.Top, FilesGrid.cells[acol,arow])
  end; 
end;

There's a long wiki article on the StringGrid: http://wiki.lazarus.freepascal.org/Grids_Reference_Page

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: Stringgrid, checkboxcell and ondrawcell
« Reply #2 on: October 20, 2014, 11:44:17 am »
Thanks WP,

Yes, you are right, i completely forget to write it in the body of the post. Yes, i use the onDrawCell. This is what i saw in different examples, and because of that i used there.

Thanks for the tip. I will move the code to the OnPrepareCanvas as well as to follow the link to the wiki.

Thanks!

Edited: It works perfect when moving the code (as proposed and adapted by @wp) to OnPrepareCanvas event. Now, the cells have the color i want but remaining the checkbox.
I will study in detail the wiki to avoid other simple questions like this. Thanks!!
« Last Edit: October 20, 2014, 12:08:08 pm by madepabloh »

 

TinyPortal © 2005-2018