Recent

Author Topic: [solved] How to read a long name-value into different valuelisteditors?  (Read 3165 times)

madepabloh

  • Full Member
  • ***
  • Posts: 160
Hi all,

I have 6 different valuelisteditor components in a form. The key columns are filled, and what i want is to read a text file containing name-value strings.

I tried this code:
Code: [Select]
procedure TMain.LoadMetadataFile;
var
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    if MetadataFile <> '' then // if the file containing the name-value strings then...
      begin
        SL.LoadFromFile(MetadataFile); // load all the name-value string into an stringlist
        for i := 1 to ValueListEditor5.RowCount-1 do //now, for the first valuelist editor, i fill the value column
          begin
            ValueListEditor5.Cells[i,1] := SL.Values[ValueListEditor5.Cells[i,0]]; // assign to the values column the value readed in the string list based on the name of the first column at the selected row.
          end;
        for i := 1 to ValueListEditor6.RowCount-1 do
          begin
            // to do the same for this an the next valuelisteditors
          end;
      end;
  finally
    SL.Free;
  end;
end;     
It compiles, butit returns and error:
Quote
Index Out of range Cell[Col=2, Row=0]

What am i doing wrong?
« Last Edit: October 31, 2014, 11:47:34 am by madepabloh »

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: how to read a long name-value into different valuelisteditors?
« Reply #1 on: October 31, 2014, 05:17:27 am »
I don't see you setting the row count for the valuelisteditor to match the row count of SL....

Also: MetadataFile <> ''   should better be 'if FileExists(MetadataFile...'
Lazarus 2.0.4/FPC 3.0.4/Win 64

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: how to read a long name-value into different valuelisteditors?
« Reply #2 on: October 31, 2014, 09:27:15 am »
Thanks, ok!, i will change this error.

About your comment... this is because the text file contains all the name-values of all the different valuelisteditors (7) i have.

If i understood correctly the wiki and other pages i readed, the next line of code should do the work:
Code: [Select]
ValueListEditor5.Cells[i,1] := SL.Values[ValueListEditor5.Cells[i,0]];
If i am right, this code assign to one selected cell of one valuelisteditor (ValueListEditor5.Cells[i,1]) one value .This value is searched in between all the list by (SL.Value). The search try to locate the value what correspond to a selected name (ValueListEditor5.Cells[i,0]). Is it true? Does the "SL.Value" procedure that search in all the StringList?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: how to read a long name-value into different valuelisteditors?
« Reply #3 on: October 31, 2014, 10:39:07 am »
I think that what you write is correct. Your problem could be that you have to check whether "i" is a valid index in ValueListEditor5:

Code: [Select]
  if i < ValueListEditor5.RowCount then 
    // ... your code

(I am not sure at the moment if the header line is included in the row count of a TValueListEditor - look in the source code, or experiment by adding/subtracting 1).

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: how to read a long name-value into different valuelisteditors?
« Reply #4 on: October 31, 2014, 11:47:19 am »
Thanks @wp for your tip.

However, this was not the solution, because the error (mine, of course) was other and so stupid.... i spent a lot of hours (and your time and patience), and the error was i changed the col and row parameters in the code.

So, this is not correct:
Code: [Select]
ValueListEditor5.Cells[i,1] := SL.Values[ValueListEditor5.Cells[i,0]];This is the correct way:
Code: [Select]
ValueListEditor5.Cells[1,i] := SL.Values[ValueListEditor5.Cells[0,i]];In fact, the error message was so clear , there was not col=2...

Now, everything works great!! Thanks so much for your time!!

 

TinyPortal © 2005-2018