Recent

Author Topic: Can we put a Tstringlist into a tstringgrid  (Read 3495 times)

WTDdallas

  • Full Member
  • ***
  • Posts: 100
Can we put a Tstringlist into a tstringgrid
« on: July 20, 2016, 04:24:24 pm »
Is there a way to have a TStringGrid of Tstringlist? 

Example a 4 X4 Grid, no fix row or column, with 16 Tstringslist displayed in it?  :o

I know it's a Long shot, but  :D

RWC

  • Jr. Member
  • **
  • Posts: 92
Re: Can we put a Tstringlist into a tstringgrid
« Reply #1 on: July 20, 2016, 06:36:34 pm »
@ WTDdallas: I’m learning Lazarus components too. I found & used the code below.
Also ‘Row’ is the currently selected row & RowCount is the number of rows in the grid including any fixed rows.
Code: Pascal  [Select][+][-]
  1. with  MyStringGrid do
  2. begin
  3.   Cells[0,0] := MyStringList1.Strings[0]; //Strings[0] is the 1st string in MyStringList1
  4.   Cells[0,1] := MyStringList2.Strings[0];
  5. //Cells[0,2]…Cells[3,3] ...by the way, how many strings do you have in each StringList?
  6.  
  7. //Also useful:
  8.   RowCount := RowCount+1; //Increment RowCount to create a new row.
  9.   RowCount := RowCount-1; //Decrement RowCount to remove a row.
  10. End;
  11.  
LAZARUS  : Lazarus-1.4.2-fpc-2.6.4-win32. OS   : Windows Vista 32bit Home Premium SP2.
CPU  : Intel Core2 Quad CPU Q6600 2.4GHz. RAM : 3GB. PCIE : NVIDIA GeForce GT610. Audo : NVIDIA HD Audio.

lainz

  • Hero Member
  • *****
  • Posts: 3696
  • Leandro Diaz
Re: Can we put a Tstringlist into a tstringgrid
« Reply #2 on: July 20, 2016, 07:00:01 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i,j,n: integer;
  4.   s: TStringList;
  5. begin
  6.   s := TStringList.Create;
  7.   s.CommaText := '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16';
  8.  
  9.   n := 0;
  10.   for i:=0 to StringGrid1.RowCount-1 do
  11.     for j:=0 to StringGrid1.ColCount-1 do
  12.     begin
  13.       StringGrid1.Cells[j, i] := s[n];
  14.       Inc(n);
  15.     end;
  16.   s.Free;
  17. end;    

WTDdallas

  • Full Member
  • ***
  • Posts: 100
Re: Can we put a Tstringlist into a tstringgrid
« Reply #3 on: July 22, 2016, 04:14:19 pm »
@ WTDdallas: I’m learning Lazarus components too. I found & used the code below.
Also ‘Row’ is the currently selected row & RowCount is the number of rows in the grid including any fixed rows.
 
//Cells[0,2]…Cells[3,3] ...by the way, how many strings do you have in each StringList?


I was wanting to display the entire stringlist in each grid, Each List could be different Sizes.

RWC

  • Jr. Member
  • **
  • Posts: 92
Re: Can we put a Tstringlist into a tstringgrid
« Reply #4 on: July 22, 2016, 06:59:08 pm »
@WTDdallas: I think I saw an example somewhere of multiple lines in a StringGrid cell, it may have been on this forum which is where I find the best (solved) examples? I also learnt much from:
http://wiki.freepascal.org/Grids_Reference_Page and
http://www.pp4s.co.uk/main/tu-form-stringgrid_demo.html

but what are you loading into your 16 stringlists? A snippet of your code would help. Could you not load everything into a single stringlist as Lainz suggest and use his nested loop example above? That’s one way I do it when I have a text file of strings separated by commas. Good luck, RWC
LAZARUS  : Lazarus-1.4.2-fpc-2.6.4-win32. OS   : Windows Vista 32bit Home Premium SP2.
CPU  : Intel Core2 Quad CPU Q6600 2.4GHz. RAM : 3GB. PCIE : NVIDIA GeForce GT610. Audo : NVIDIA HD Audio.

 

TinyPortal © 2005-2018