2

I'm trying to parse a string (sys) that looks exactly like this

-1|low
0|normal
1|high

I need to pair them in a combo box, where for example, low is the caption and -1 will be the value. What is the best way to do this? What I have so far is:

 var
 sys : String;
 InputLine : TStringList;

   InputLine := TStringList.Create;
   InputLine.Delimiter := '|';
   InputLine.DelimitedText := sys;
   Combobox1.items.AddStrings(InputLine);
   FreeAndNil(InputLine)

This gives each line of the combo box as such:

 -1
 low
 0
 normal
 1
 high
Ken White
  • 123,280
  • 14
  • 225
  • 444
user1868232
  • 623
  • 2
  • 10
  • 21

1 Answers1

3

Parse it manually yourself.

var
  SL: TStringList;
  StrVal: string;
  IntVal: Integer;
  Line: string;
  DividerPos: Integer;
begin
  SL := TStringList.Create;
  try
    SL.LoadFromFile('Whatever.txt');
    for Line in SL do
    begin
      DividerPos := Pos('|', Line);
      if DividerPos > 0 then
      begin
        StrVal := Copy(Line, DividerPos + 1, Length(Line));
        IntVal := StrToInt(Copy(Line, 1, DividerPos - 1));
        ComboBox1.Items.AddObject(StrVal, TObject(IntVal));
      end;
    end
  finally
    SL.Free;
  end;
end;

To retrieve the value from a selected item:

if (ComboBox1.ItemIndex <> -1) then
  SelVal := Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
Ken White
  • 123,280
  • 14
  • 225
  • 444