arrayslistdelphifiremonkeytlistbox

Accessing to the TextSettings properties of a TListBox Item


I have an array of strings that I want to put in a TListBox (FMX) for selection. The properties of a TListBox do not include a TextSetting properties for the lines, but I have seen that each of the TList item has it. How can I access to each line as an object? I tried this

   BatchSets_ListBox.Items.BeginUpdate;
   BatchSets_ListBox.Items.Clear;
   For i := 0 to (Length(MyList) - 1) do
   Begin
      BatchSets_ListBox.Items.Add(MyList[i].BATCH_SET_NAME);
      BatchSets_ListBox.ListItems[BatchSets_ListBox.Items.Count].TextSettings.Font.Size := 18;
   End;
   BatchSets_ListBox.Items.EndUpdate;

but it doesn't work, it says ACCESS VIOLATION on the line that tries to access ListItems. What mm I doing wrong? The TLitBox shouldn't have a ListItems objects in it that stores the Items? Thanks for any help.


Solution

  • Your problem is this line:

    BatchSets_ListBox.ListItems[BatchSets_ListBox.Items.Count].TextSettings.Font.Size := 18;
    

    You are accessing an item that doesn't exist. Valid indices for a TListBox.ListItems is [0..Count-1] not [1..Count], ie. the index starts with 0, so a listbox with two items has valid indices of 0 and 1.

    Also, if MyList is a TArray<> of some kind, you should iterate over it as this:

    For i := Low(MyList) to High(MyList) do ...
    

    The troublesome line should be coded as follows:

    BatchSets_ListBox.ListItems[I-Low(MyList)].TextSettings.Font.Size := 18;