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
2
  • put your stringlist into for and form your combo with odd/even values of index of a for loop Commented Feb 13, 2013 at 0:33
  • I don't see how you are struggling. You've already split the values. All you need to do is add them to the combo box. Commented Feb 13, 2013 at 1:54

1 Answer 1

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]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.