Board index » delphi » TStringGrid - OnDrawCell Event

TStringGrid - OnDrawCell Event

Using D5, I have a TStringGrid component that has a one fixed row at the
top and three columns, goRowSelect property is set to true so that the
entire row is selected when any cell within that row is selected.  All
non-fixed rows are initially shown in Bold.  Once the row is selected
(either by moving the cursor or clicking with mouse) the row is to be
shown so that it is not bold anymore.  I have written the following code
in the OnDrawCell event of the TStringGrid component:

procedure grdMessagesDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);

// the first row is fixed so don't worry about changing from bold
if (ARow > 0) then begin
  with grdMessages.Canvas do begin
    Font.Style := Font.Style - [fsBold];
    TextRect(Rect, Rect.Left + 2, Rect.Top + 2, grdMessages.Cells[ACol,
ARow]);
    TextOut(Rect.Left + 2, Rect.Top + 2, grdMessages.Cells[ACol, ARow]);

  end;
end;

This seems to work perfectly when the row is selected by scrolling up
and down with the cursor.  But when I click on the row in question, only
the cell that is clicked changes to non-bold, while the other cells
(columns) in that row remain bold.  How can I alter the code so that
when I click on a cell within a row, all of the cells (columns) in that
row will be set to non-bold as well instead of just the selected cell
(column).

    Thanks in advance,
        David Platt

 

Re:TStringGrid - OnDrawCell Event


Quote
In article <3BF63B39.267E3...@cableone.net>, David Platt wrote:
> Using D5, I have a TStringGrid component that has a one fixed row at the
> top and three columns, goRowSelect property is set to true so that the
> entire row is selected when any cell within that row is selected.  All
> non-fixed rows are initially shown in Bold.  Once the row is selected
> (either by moving the cursor or clicking with mouse) the row is to be
> shown so that it is not bold anymore.  I have written the following code
> in the OnDrawCell event of the TStringGrid component:

You are missing one step: you are not erasing the cell before you draw the
text. When the OnDrawCell event is fired the cell has already been
completely drawn (unless you set DefaultDrawing to false, in which case
you have to draw everything, including grid lines and fixed cells).
You are also drawing the text twice, which is a waste of virtual ink <g>.

Quote
> procedure grdMessagesDrawCell(Sender: TObject; ACol, ARow: Integer;
> Rect: TRect; State: TGridDrawState);

> // the first row is fixed so don't worry about changing from bold
> if (ARow > 0) then begin
>   with grdMessages.Canvas do begin

      fillrect( rect );

Quote
>     Font.Style := Font.Style - [fsBold];
>     TextRect(Rect, Rect.Left + 2, Rect.Top + 2, grdMessages.Cells[ACol,
> ARow]);
>   end;
> end;

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:TStringGrid - OnDrawCell Event


On Sat, 17 Nov 2001 03:26:01 -0700, David Platt

Quote
<davepl...@cableone.net> wrote:
>How can I alter the code so that
>when I click on a cell within a row, all of the cells (columns) in that
>row will be set to non-bold as well instead of just the selected cell
>(column).

Looks to me that handling this at OnDrawColumnCell would do it.

Manuel Algora
m...@encomix.es

Re:TStringGrid - OnDrawCell Event


There is no OnDrawColumnCell event for the TStringGrid component.
Quote
Manuel Algora wrote:
> On Sat, 17 Nov 2001 03:26:01 -0700, David Platt
> <davepl...@cableone.net> wrote:

> >How can I alter the code so that
> >when I click on a cell within a row, all of the cells (columns) in that
> >row will be set to non-bold as well instead of just the selected cell
> >(column).

> Looks to me that handling this at OnDrawColumnCell would do it.

> Manuel Algora
> m...@encomix.es

Re:TStringGrid - OnDrawCell Event


You state that I need to erase the cell before I draw the text.  Where do I do
this...in the row moved and cell click events or do I do it in the drawcell
event?  Also, when would I set the text back to what it needs to be after I
clear it, and where would I do this?  I am not sure of how to proceed.

    Thank you for any more assistance,
        David Platt

Quote
"Peter Below (TeamB)" wrote:
> In article <3BF63B39.267E3...@cableone.net>, David Platt wrote:
> > Using D5, I have a TStringGrid component that has a one fixed row at the
> > top and three columns, goRowSelect property is set to true so that the
> > entire row is selected when any cell within that row is selected.  All
> > non-fixed rows are initially shown in Bold.  Once the row is selected
> > (either by moving the cursor or clicking with mouse) the row is to be
> > shown so that it is not bold anymore.  I have written the following code
> > in the OnDrawCell event of the TStringGrid component:

> You are missing one step: you are not erasing the cell before you draw the
> text. When the OnDrawCell event is fired the cell has already been
> completely drawn (unless you set DefaultDrawing to false, in which case
> you have to draw everything, including grid lines and fixed cells).
> You are also drawing the text twice, which is a waste of virtual ink <g>.

> > procedure grdMessagesDrawCell(Sender: TObject; ACol, ARow: Integer;
> > Rect: TRect; State: TGridDrawState);

> > // the first row is fixed so don't worry about changing from bold
> > if (ARow > 0) then begin
> >   with grdMessages.Canvas do begin
>       fillrect( rect );
> >     Font.Style := Font.Style - [fsBold];
> >     TextRect(Rect, Rect.Left + 2, Rect.Top + 2, grdMessages.Cells[ACol,
> > ARow]);
> >   end;
> > end;

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No e-mail responses, please, unless explicitly requested!
> Note: I'm unable to visit the newsgroups every day at the moment,
> so be patient if you don't get a reply immediately.

Re:TStringGrid - OnDrawCell Event


On Sun, 18 Nov 2001 02:14:54 -0700, David Platt

Quote
<davepl...@cableone.net> wrote:
>There is no OnDrawColumnCell event for the TStringGrid component.

Ooops, sorry.

Manuel Algora
m...@encomix.es

Re:TStringGrid - OnDrawCell Event


Quote
In article <3BF78B6E.21DD1...@cableone.net>, David Platt wrote:
> You state that I need to erase the cell before I draw the text.  Where do I do
> this...in the row moved and cell click events or do I do it in the drawcell
> event?  

Read the whole of the reply i gave you <g>. I inserted the needed FillRect line
into the code snippet you posted.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:TStringGrid - OnDrawCell Event


I did insert the FillRect command as you recommended doing.  However, the results
were exactly the same as the initial problem that I reported.

    - David Platt

Quote
"Peter Below (TeamB)" wrote:
> In article <3BF78B6E.21DD1...@cableone.net>, David Platt wrote:
> > You state that I need to erase the cell before I draw the text.  Where do I do
> > this...in the row moved and cell click events or do I do it in the drawcell
> > event?

> Read the whole of the reply i gave you <g>. I inserted the needed FillRect line
> into the code snippet you posted.

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No e-mail responses, please, unless explicitly requested!
> Note: I'm unable to visit the newsgroups every day at the moment,
> so be patient if you don't get a reply immediately.

Re:TStringGrid - OnDrawCell Event


I have decided to set the defaultdrawing property to false and just draw everything
manually.  I have been able to do this without any major problems, except setting
the top row (fixed) to 3D appearance, but I used the API calls - DrawEdge and
InflateRect to solve that problem.  Thank you for your assistance.

    - David Platt

Quote
"Peter Below (TeamB)" wrote:
> In article <3BF78B6E.21DD1...@cableone.net>, David Platt wrote:
> > You state that I need to erase the cell before I draw the text.  Where do I do
> > this...in the row moved and cell click events or do I do it in the drawcell
> > event?

> Read the whole of the reply i gave you <g>. I inserted the needed FillRect line
> into the code snippet you posted.

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No e-mail responses, please, unless explicitly requested!
> Note: I'm unable to visit the newsgroups every day at the moment,
> so be patient if you don't get a reply immediately.

Re:TStringGrid - OnDrawCell Event


Quote
In article <3BF7FDE0.CB461...@cableone.net>, David Platt wrote:
> I did insert the FillRect command as you recommended doing.  However, the results
> were exactly the same as the initial problem that I reported.

That should not be the case. Anyway, if you found a way around the problem, so much
the better.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Other Threads