0

im start with Delphi. I have a problem with TStringGrid and Colored the Cell. im using this code for color the BackGround when is selected:

procedure TForm_Matrix.MatrizGeneralDrawCell(Sender: TObject;
  ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  ACol:=MatrizGeneral.Col;
  ARow:=MatrizGeneral.Row;
  begin
    if (RBAlto.Checked = True) then // Nivel de color ROJO - ALTO
      MatrizGeneral.Canvas.Brush.Color :=clRed;
      MatrizGeneral.Canvas.FillRect(Rect);
    if (RBMedio.Checked = True) then
      MatrizGeneral.Canvas.Brush.Color :=clYellow;
      MatrizGeneral.Canvas.FillRect(Rect);
    if (RBBajo.Checked = True) then
      MatrizGeneral.Canvas.Brush.Color :=clLime;
      MatrizGeneral.Canvas.FillRect(Rect);
  end;
end;

Its work, but when i try to change the color change the cell selected, and the first cell idk why.

  1. When i select 3 cell with color red. (Work Fine) enter image description here

  2. Change the color of another cell, change the fist cell T.T enter image description here

    https://i.stack.imgur.com/umG0r.png https://i.stack.imgur.com/1o93C.png

HELP!!!

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
Wixo
  • 3
  • 7

2 Answers2

2

If you want to color only the selected cells, you have to check the State parameter that is passed in, and only draw if the State is selected.

Also, you are drawing the cell 3x in that routine . Just put MatrizGeneral.Canvas.FillRect(Rect); once at the end, you do not need it with every IF block.

Dave Novo
  • 693
  • 3
  • 9
  • I use this rutine for colored the cell selected with a radiogroup: if MatrizGeneral.Cells[ACol,ARow] <> '' then begin case StrToInt(MatrizGeneral.Cells[ACol,ARow]) of 0: BGColor := clRed; 1: BGColor := clYellow; 2: BGColor := clLime; else BGColor := clWhite; end; with MatrizGeneral do begin Canvas.Brush.Color := BGColor; Canvas.FillRect(Rect); if (gdFocused in State) then Canvas.Font.Color := clWhite else Canvas.Font.Color := clBlack; end; end; – Wixo Mar 19 '14 at 14:11
0

I use this rutine for colored the cell selected with a radiogroup:

if MatrizGeneral.Cells[ACol,ARow] <> '' then begin
case StrToInt(MatrizGeneral.Cells[ACol,ARow]) of
  0: BGColor := clRed;
  1: BGColor := clYellow;
  2: BGColor := clLime;
else
  BGColor := clWhite;
end;
with MatrizGeneral do begin
  Canvas.Brush.Color := BGColor;
  Canvas.FillRect(Rect);

  if (gdFocused in State) then
    Canvas.Font.Color := clWhite
  else
    Canvas.Font.Color := clBlack;
end;

end;

Works Great!

Wixo
  • 3
  • 7