Filling a ListBox lazarus / 31-LCL ---------------------------------------------------------------------- Neuromancer · 2014-04-13 09:15 Hello, I am trying to fill a TListBox with a TStringList : Code: [Select]procedure TfrmMain.FormShow(Sender : TObject); var     i : Integer;     LFilters : TStringList; begin     LoadFile(GetCurrentDir + DirectorySeparator + FILENAME);     LFilters := GetFilters;     for i:=0 to LFilters.count-1 do     begin         WriteLn(LFilters.Strings[i]);         lbFilters.Items.Add(LFilters.Strings[i]);     end;     LFilters.Free; end; But it does not work : lbFilters is filled with blank lines. The strings are correctly displayed with WriteLn... ---------------------------------------------------------------------- Blaazen · 2014-04-13 10:19 I see nothing wrong with your code. Of course, I don't know what GetFilters method does. You can try simplier way: Code: [Select]lbFilters.Items.Assign(LFilters);instead of loop. Also, I don't see LFilters:=TStringList.Create ---------------------------------------------------------------------- Neuromancer · 2014-04-13 12:08 It does not work better : Code: [Select]procedure TfrmMain.FormShow(Sender : TObject); var     i : Integer;     LFilters : TStringList; begin     LoadFile(GetCurrentDir + DirectorySeparator + FILENAME);     LFilters := GetFilters;     lbFilters.Items.Assign(LFilters);     LFilters.Free; end; QuoteAlso, I don't see LFilters:=TStringList.CreateThe list is created in the GetFilters function (see udirectory.pas). The strings are loaded from a file. ---------------------------------------------------------------------- howardpc · 2014-04-13 12:58 You're reinventing the wheel, and not doing it correctly. The functionality you want is already built in to TStringList. No specialization of a generic class is needed. A simple OnCreate handler in your main form (no UDictionary needed at all) as follows will do what you want: Code: [Select]procedure TForm1.FormCreate(Sender: TObject); var   sl:TStringList;   i: integer; begin   sl:=TStringList.Create;   try     sl.NameValueSeparator:='|';     sl.LoadFromFile('dictionnaire.txt');     for i:=0 to sl.Count-1 do       lbFilters.Items.Add(sl.Names[i]);   finally     sl.Free;   end; end; ---------------------------------------------------------------------- engkin · 2014-04-13 17:32 While TStringList index starts from 0, String index starts from 1. Your SplitString function makes this mistake: Code: [Select]function SplitString(AString : String; const ASeparator : Char) : TStringList; ..     for i := 0 to Length(AString) + 1 do ..             LTempString := LTempString + AString[i]; Simply change it to start from 1: Code: [Select]    for i := 1 to Length(AString) + 1 do Edit: You have another mistake. You let index variable i go beyond Length(AString) then you check for the separator before checking for length Code: [Select]        if (AString[i] = ASeparator) or (i > Length(AString)) then this order needs to be reversed: Code: [Select]        if (i > Length(AString)) or (AString[i] = ASeparator) then These two changes would give the following SplitString function: Code: [Select]function SplitString(AString : String; const ASeparator : Char) : TStringList; var     i, LPosition : Integer;     LTempString : String; begin     LTempString := '';     Result := TStringList.Create;     for i := 1 to Length(AString) + 1 do     begin         if (i > Length(AString)) or (AString[i] = ASeparator) then         begin             Result.Add(LTempString);             LTempString := '';         end         else             LTempString := LTempString + AString[i];     end; end; ---------------------------------------------------------------------- zeljko · 2014-04-13 17:45 Quote from: engkin on April 13, 2014, 07:32:53 pmWhile TStringList index starts from 0, String index starts from 1. Your SplitString function makes this mistake: Code: [Select]function SplitString(AString : String; const ASeparator : Char) : TStringList; ..     for i := 0 to Length(AString) + 1 do ..             LTempString := LTempString + AString[i]; Simply change it to start from 1: Code: [Select]    for i := 1 to Length(AString) + 1 do No, it's range error. It should be for i := 1 to length(AString) // without +1 ---------------------------------------------------------------------- engkin · 2014-04-13 17:51 Quote from: zeljko on April 13, 2014, 07:45:37 pmNo, it's range error. It should be for i := 1 to length(AString) // without +1 The OP took care of it with (i > Length(AString) condition but needed to be checked first. Most likely because of the possibility of having a separator as the last character in the string.