-1

i have report in TStringGrid and need that when press space key, change bg color of selected horizontal line cells.

how can do that

zuluman
  • 49
  • 7
  • 3
    Remember the row states (in some collection) and when the user presses space key, update that state and call `Invalidate` for the grid. Finally, in the `OnDrawCell` event draw the cell of the row by its state. – TLama Sep 06 '15 at 22:55
  • 1
    [Setting background color of selected row on TStringGrid](http://stackoverflow.com/q/5575713/62576) doesn't help you? – Ken White Sep 06 '15 at 22:56

1 Answers1

0
      TSelColor = class
      public
        Color: TColor;
        constructor Create(const aColor: TColor);
      end;

      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
      private
        { Private declarations }
      public
        kPressed: boolean;
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      yourColor: TColor;
    begin
      yourColor:= clRed;
      if gdFixed in State then
        TStringGrid(Sender).Canvas.Brush.Color:= clBtnFace
      else if gdSelected in State then
      begin
        TStringGrid(Sender).Canvas.Brush.Color:= clAqua;
        if kPressed and not (TStringGrid(Sender).Objects[ACol, ARow] is TSelColor) then
          TStringGrid(Sender).Objects[ACol, ARow]:= TSelColor.Create(yourColor)
        else if kPressed and (TStringGrid(Sender).Objects[ACol, ARow] is TSelColor) then
          TStringGrid(Sender).Objects[ACol, ARow]:= nil;
      end
      else
      begin
        TStringGrid(Sender).Canvas.Brush.Color:= clWindow;
        if TStringGrid(Sender).Objects[ACol, ARow] is TSelColor then
          TStringGrid(Sender).Canvas.Brush.Color:= TSelColor(TStringGrid(Sender).Objects[ACol, ARow]).Color;
      end;
      TStringGrid(Sender).Canvas.FillRect(Rect);
      TStringGrid(Sender).Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, TStringGrid(Sender).Cells[ACol, ARow]);
    end;

    procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      if Key = 32 then
      begin
        kPressed:= true;
        StringGrid1.Repaint;
        kPressed:= false;
      end;
    end;

    { TSelColor }

    constructor TSelColor.Create(const aColor: TColor);
    begin
      inherited Create;
      Color:= aColor;
    end;

StringGrid1
DefaultDrawning [false] Options.goRowSelect [true]

scribe
  • 163
  • 1
  • 10