Table of Contents

Data & persistence

Beyond holding text, the list box moves items in and out of external storage: TStrings, files, streams, and the clipboard. It also carries arbitrary application data on each item through the data fields introduced in Items. Use these features to seed a list from a memo or file, to save the user's edits, to round-trip a list through the clipboard, or to map each row back to a domain object on selection. The text-based load/save helpers write a plain format that is compatible with standard list controls.

Strings, files, and streams

LoadFromStrings/SaveToStrings move item text to and from any TStrings (a memo, a TStringList); LoadFromFile/SaveToFile and LoadFromStream/SaveToStream persist to disk or a stream. The ATextOnly parameter on the save helpers keeps the output compatible with plain list controls when set to True:

procedure TForm1.LoadFromMemo;
begin
  // Each line of the memo becomes one item.
  ListBox1.LoadFromStrings(Memo1.Lines);
end;

procedure TForm1.SaveToMemo;
begin
  ListBox1.SaveToStrings(Memo1.Lines);
end;

procedure TForm1.LoadFromDisk;
begin
  ListBox1.LoadFromFile('items.txt');
end;

procedure TForm1.SaveToDisk;
begin
  // ATextOnly := True keeps the file compatible with plain list controls.
  ListBox1.SaveToFile('items.txt', True);
end;

procedure TForm1.CopySelection;
begin
  ListBox1.CopyToClipboard(False);   // include appearance and item data
end;

procedure TForm1.PasteSelection;
begin
  ListBox1.PasteFromClipboard;
end;
Member Purpose
LoadFromStrings / SaveToStrings Round-trip item text through a TStrings
LoadFromFile / SaveToFile Persist items to a text file (ATextOnly for plain format)
LoadFromStream / SaveToStream Persist items to a stream
Items.AddStrings Append (or replace) items from a string list

Per-item application data

Each TTMSFNCListBoxItem exposes DataString, DataInteger, DataBoolean, DataObject, and DataPointer, plus DBKey and Tag. These let a row stand in for a record without a parallel lookup structure — attach an object or key when you build the list, then read it back in OnItemSelected:

procedure TForm1.ListBox1ItemSelected(Sender: TObject;
  AItem: TTMSFNCListBoxItem);
begin
  if AItem.DataObject is TContact then
    ShowContact(TContact(AItem.DataObject));
end;
Note

DataObject and DataPointer are references only — the list box does not own or free them. Manage their lifetime yourself.

Clipboard round-trip

When Interaction.ClipboardMode is lcmTextOnly or lcmFull, the CopyToClipboard, CutToClipboard, and PasteFromClipboard methods move the selected items through the system clipboard. With lcmFull, item data and appearance travel with the text; with lcmTextOnly, only the text does. The OnBefore…/OnAfter… clipboard events (see Events) let you veto or react to each operation.

Combining persistence with data fields

A common pattern combines both halves of this page: load item text, then attach a domain key and object to each row so selection can resolve the full record. Because SaveToStrings writes only text, keep the authoritative data in your model and use the list for display and selection:

procedure TForm1.LoadContacts(AContacts: TList<TContact>);
var
  I: Integer;
begin
  ListBox1.BeginUpdate;
  try
    // 1. Load the display text from any TStrings source.
    ListBox1.Items.Clear;
    for I := 0 to AContacts.Count - 1 do
      ListBox1.Items.Add(AContacts[I].DisplayName);

    // 2. Attach the stable key and the backing object to each row.
    for I := 0 to ListBox1.Items.Count - 1 do
    begin
      ListBox1.Items[I].DBKey := AContacts[I].Id;
      ListBox1.Items[I].DataObject := AContacts[I];
    end;
  finally
    ListBox1.EndUpdate;
  end;
end;

Pitfalls

  • SaveToFile/SaveToStream with ATextOnly := True persist only item text — icons, colors, and data fields are not saved. Use False to retain item attributes where the format supports it.
  • The clipboard methods do nothing while Interaction.ClipboardMode is lcmNone.
  • The list box does not free objects referenced by DataObject/DataPointer.
  • TTMSFNCListBoxLoadFromStrings, SaveToStrings, LoadFromFile, SaveToFile, LoadFromStream, SaveToStream, CopyToClipboard, CutToClipboard, PasteFromClipboard

See also

  • Items — the data fields and DBKey referenced here
  • Events — clipboard events that pair with the clipboard methods
  • InteractionClipboardMode and selection