Board index » delphi » TStringGrid selection rectangle

TStringGrid selection rectangle

I have a problem with the persistence, in the sense that it won't go
away, of the selection rectangle in the TStringGrid component. If the
grid is used for displaying results in a tabular form, the default
behaviour is for the top left cell to be selected when a StringGrid is
displayed. How can it be made to disappear? I've resorted to this code
in the OnPaint event,

var
  SRect: TGridRect;
begin
  SRect.Top := -1;
  SRect.Left := -1;
  SRect.Bottom := -1;
  SRect.Right := -1;
  Form1.StringGrid1.Selection := SRect;
end;

which gets rid of it, but surely there is a more elegant solution? I
seem to have tried the obvious things in the Options settings to no
avail.
--
Barney Tyrwhitt-Drake

 

Re:TStringGrid selection rectangle


Quote
Barney Tyrwhitt-Drake <bar...@tdrake.demon.co.uk> wrote:
>I have a problem with the persistence, in the sense that it won't go
>away, of the selection rectangle in the TStringGrid component. If the
>grid is used for displaying results in a tabular form, the default
>behaviour is for the top left cell to be selected when a StringGrid is
>displayed. How can it be made to disappear? I've resorted to this code
>in the OnPaint event,
>var
>  SRect: TGridRect;
>begin
>  SRect.Top := -1;
>  SRect.Left := -1;
>  SRect.Bottom := -1;
>  SRect.Right := -1;
>  Form1.StringGrid1.Selection := SRect;
>end;
>which gets rid of it, but surely there is a more elegant solution? I
>seem to have tried the obvious things in the Options settings to no
>avail.
>--
>Barney Tyrwhitt-Drake

You can use the OnDrawCell event, but then you must draw the text you
want in each cell. By ignoring the State variable and always using the
default Brush, you can get rid of the selection. An advantage to
drawing your own text in each cell is that you can center or right
justify the text if you wish. You can find a code example in the
freeware VCL component NEWCAL.ZIP located at either Grumpfish or the
Delphi Super Page. There are links to each site from my WWW page.

Barry.

/---------------------------------------------------------------\
| Check out CanalRun's WWW page http://www.vcomm.net/~canalrun/ |
| Education, Investment, Personal Productivity Shareware.       |
\---------------------------------------------------------------/

Re:TStringGrid selection rectangle


Try this:

1. Disable Default Drawing.
2. OnDrawCell Code

MyGrid.Canvas.TextRect
    (Rect, Rect.Left, Rect.Top, MyGrid.Cells[Col,Row]);  
if condition then
    MyGrid.Canvas.DrawFocusRect(Rect);

Mark.

----------------------------------------------------------

Quote
canal...@vcomm.net (Barry) wrote:
Barney Tyrwhitt-Drake <bar...@tdrake.demon.co.uk> wrote:
>I have a problem with the persistence, in the sense that it won't go
>away, of the selection rectangle in the TStringGrid component. If the
>grid is used for displaying results in a tabular form, the default
>behaviour is for the top left cell to be selected when a StringGrid is
>displayed. How can it be made to disappear? I've resorted to this code
>in the OnPaint event,
>var
>  SRect: TGridRect;
>begin
>  SRect.Top := -1;
>  SRect.Left := -1;
>  SRect.Bottom := -1;
>  SRect.Right := -1;
>  Form1.StringGrid1.Selection := SRect;
>end;
>which gets rid of it, but surely there is a more elegant solution? I
>seem to have tried the obvious things in the Options settings to no
>avail.
>--
>Barney Tyrwhitt-Drake

You can use the OnDrawCell event, but then you must draw the text you
want in each cell. By ignoring the State variable and always using the
default Brush, you can get rid of the selection. An advantage to
drawing your own text in each cell is that you can center or right
justify the text if you wish. You can find a code example in the
freeware VCL component NEWCAL.ZIP located at either Grumpfish or the
Delphi Super Page. There are links to each site from my WWW page.

Barry.

/---------------------------------------------------------------\
| Check out CanalRun's WWW page http://www.vcomm.net/~canalrun/ |
| Education, Investment, Personal Productivity Shareware.       |
\---------------------------------------------------------------/

Other Threads