Board index » delphi » Newbie with StringGrid problem

Newbie with StringGrid problem

I really can't understand why I'm having such a stupid problem, but I
am.  I am initializing my StringGrid through a nested for loop similar
to the following (in the OnDrawCell event handler):

for x := 0 to ColCount - 1 do
  for y := 0 to RowCount -1 do
        StringGrid1.Cells[x,y] := 'whatever';

For some reason, the loop finishes and the event handler begins
again--it never ends.  The text in each cell flickers every
half-second or so as it is re-initialized.  I've tried it with
different numbers of columns and rows, and have tried substituting the
actual numbers instead of ColCount and RowCount with no luck.  When I
trace the program step by step, the loop finishes and the entire
OnDrawCell event handler is begun again (not just the loop).

Anyone know what I'm doing wrong?

Liam Quinn
aqu...@hookup.net
http://www.hookup.net/~aquinn/liam.html

 

Re:Newbie with StringGrid problem


On 11 Aug 95 23:51:31 GMT, aqu...@hookup.net (Liam Quinn) wrote:

Quote
> I really can't understand why I'm having such a stupid problem, but I
> am.  I am initializing my StringGrid through a nested for loop similar
> to the following (in the OnDrawCell event handler):
> for x := 0 to ColCount - 1 do
>   for y := 0 to RowCount -1 do

        StringGrid1.Cells[x,y] := 'whatever';

Quote
> For some reason, the loop finishes and the event handler begins
> again--it never ends.  The text in each cell flickers every
> half-second or so as it is re-initialized.

[snip]

Liam, try putting the nested loop elsewhere, outside the OnDrawCell
event handler (maybe FormCreate?).  Each time you store a value
in a StringGrid Cell your application will mark the grid as "dirty",
preparing
for a call to the grid's Paint method which will be executed when your
code finishes.  The very act of setting
    StringGrid1.Cells[x,y] := 'whatever';
tells your proggie to draw 'whatever' in the [x,y] location of StringGrid1.
When the cell is drawn, the OnDrawCell event handler is called, which
starts the process all over again by storing a value in each cell again.

I wish I could state it more clearly.  Heck, I wish the online help could!
I constantly remind myself of Disraeli's comment: "Adversity is the
best educator."  With Delphi, you can learn an awful lot!  --Joel Guerra

Re:Newbie with StringGrid problem


Quote
aqu...@hookup.net (Liam Quinn) wrote:
>I really can't understand why I'm having such a stupid problem, but I
>am.  I am initializing my StringGrid through a nested for loop similar
>to the following (in the OnDrawCell event handler):

>for x := 0 to ColCount - 1 do
>  for y := 0 to RowCount -1 do
>    StringGrid1.Cells[x,y] := 'whatever';

>For some reason, the loop finishes and the event handler begins
>again--it never ends.  The text in each cell flickers every
>half-second or so as it is re-initialized.  I've tried it with
>different numbers of columns and rows, and have tried substituting the
>actual numbers instead of ColCount and RowCount with no luck.  When I
>trace the program step by step, the loop finishes and the entire
>OnDrawCell event handler is begun again (not just the loop).

>Anyone know what I'm doing wrong?

>Liam Quinn
>aqu...@hookup.net
>http://www.hookup.net/~aquinn/liam.html

Probably the fact that you're changing the contents of the cells is causing them to
be redrawn, resulting in your getting an OnDrawCell event for each cell that you change,
resulting in your changing the cells again, etc, etc, etc.  You should try to
change the contents of the cells (if it's really necessary) in some other event, like
OnClick, or OnDblClick or something.
Hope this helps.

-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------

Other Threads