Recent

Author Topic: [solved] Safely "empty" a stringgrid?  (Read 7527 times)

squirreldancer

  • New member
  • *
  • Posts: 7
[solved] Safely "empty" a stringgrid?
« on: December 03, 2013, 05:05:47 pm »
I have two stringgrids on different tabs of a page control.
For stringgrid1, I issue stringgrid1.clear, then fill the grid's cells, by column and row within 2 nested for loops, parsing the cell data from a tstringlist.
After processing the stringgrid1, I want to move to the tab containing stringgrid2.
I issue stringgrid2.clear, then attempt to copy a portion of stringrid1 to stringgrid2, cell by cell, using nested for loops.
The result is an error, that stringrid2[0,0] is out of range.
If I don't use stringgrid2.clear, it works.
Both stringgrids are identical.
Because the user can flip back and forth between stringgrids 1&2, I need to be able to initialize both, each time stringrid1 is changed.
However, it seems like while stringgrid1.clear initializes it correctly, stringgrid2.clear removes all of the grid's rows and columns and doesn't permit me to copy in new data.

I realize there are many possibilities, but any ideas why stringgrid.clear is behaving differently for my two stringgrids? Which is the "correct" behaviour?

Do I need to fill my stringgrid2 with empty spaces to ensure it is empty?
« Last Edit: December 04, 2013, 10:28:45 pm by squirreldancer »

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Safely "empty" a stringgrid?
« Reply #1 on: December 03, 2013, 05:14:13 pm »
You need to set StringGrid2.RowCount:= StringGrid1.RowCount. after you clear StrignGrid2. 

You also need to Set StringGrid2 Columns to the number of Columns you will be using, after you clear StrignGrid2. 
Lazarus Trunk / fpc 2.6.2 / Win32

squirreldancer

  • New member
  • *
  • Posts: 7
Re: Safely "empty" a stringgrid?
« Reply #2 on: December 03, 2013, 08:33:35 pm »
Thanks. That makes sense, but why can I write my strings to stringgrid1 after clearing it, but not from stringgrid1 to stringgrid2 after I clear stringgrid2? Isn't a string a string?

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Safely "empty" a stringgrid?
« Reply #3 on: December 03, 2013, 08:39:59 pm »
TStringGrid.Clear resets the TStringGid.ColCount and RowCount to Zero so there is no place to add your strings.

Put a TStrignGrid (StringGrid1) on a Form with ColCount:= 10 and RowCount:= 10.

Add a Button and in the OnClick Event:  StringGrid1.Clear

You will see then why you can't add Strings.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018