Cazhou
Cazhou

Reputation: 11

Listbox set Item.Detail at runtime

I am new to Stack overflow and new to Delphi XE7.

I am stuck at a particular part of my programming; I have been working on a Multi Device Application and I formatted a listbox using livebindings designer. The listbox shows the first line of an address (house) which is under Item.text in livebindings and the Postcode to the property is in Item.detail in livebindings. Therefore each entry in the listbox has the first line and the postcode of the address which are taken from an internal database.

When I add a new address to the program I have managed to update the database with the full address and the listbox with the first line of the address at runtime, however I do not know how to access the Item.detail part at runtime to include the postcode. Any help would be much appreciated

Upvotes: 1

Views: 5791

Answers (3)

Good day, to fill a ListBox from a ClientDataSet can be done with the following procedure

procedure TfPpal.setListBox;
var
  ListItem : TListBoxItem;
begin
  ListBox.BeginUpdate;
  cdsDataSet.First;
  while not cdsDataSet.Eof do
  begin
    ListItem := TListBoxItem.Create(ListBox);
    ListItem.Text        := cdsDataSet.FieldByName('id').Text;
    ListItem.ImageIndex  := 1;
    ListItem.Height      := 61;
    ListItem.StyleLookup := 'listboxitemnodetail';
    ListItem.ItemData.Accessory := TListBoxItemData.TAccessory.aMore;
    LisxBox.AddObject(ListItem);
    cdsDataSet.Next;
  end;
  lbxLisxBox.Height  := 50 + (cdsDataSet.RecNo * 61);
  ListBox.EndUpdate;
end;

Upvotes: 2

Cazhou
Cazhou

Reputation: 11

Thanks to all who answered my question. First time on Stack Overflow. I am posting the final code which is now working to help anyone who may be looking for a similar result.

procedure TForm1.Button2Click(Sender: TObject);
Var
  Item: TListBoxItem;
begin
  Layout13.Visible := False;
  Layout2.Visible := True;
  MultiView1.Enabled := True;
  Button1.Enabled := True;
  ButtonDelete1.Enabled := True;
  Button2.Enabled := False;
     // setup SQLite in-memory connection
  FDConnection1.DriverName := 'SQLite';
  FDConnection1.Connected := True;
with fdqueryInsert do
  begin
    // write new address to database
    Insert;
    fieldbyname('Address1').asstring := edit1.text;
    fieldbyname('Address2').asstring := edit2.text;
    fieldbyname('City').asstring := edit3.text;
    fieldbyname('County').asstring := edit4.text;
    fieldbyname('Postcode').asstring := edit5.text;
    post;
    close;
    open;
  End;
    // draw address line and postcode to listbox
    ListBox2.Clear;
    ListBox2.BeginUpdate;
    FDQueryUpdate.Close;
    FDQueryUpdate.SQL.Text := 'SELECT Address1, Postcode FROM Address';
    try
      FDQueryUpdate.Open;
      Item := TlistBoxItem.Create(ListBox2);
      while not FDQueryUpdate.Eof do
      begin
      // create and format listbox to show bottomdetail
        Item := TlistBoxItem.Create(ListBox2);
        Item.StyleLookup := 'listboxitembottomdetail';
        // draw address to text part and postcode to bottom detail of Listbox item
        Item.Text := (FDQueryUpdate.Fields[0].AsString);
        Item.ItemData.Detail := (FDQueryUpdate.Fields[1].AsString);;
        ListBox2.AddObject( Item );
        FDQueryUpdate.Next;
      end;
    finally
    ListBox2.EndUpdate;
    FDQueryUpdate.Close;
  end;
  // clear editboxes and reset for next address input
  Button7Click(Sender);
end;

Upvotes: 0

LU RD
LU RD

Reputation: 34919

"... how to access the Item.detail part at runtime"

See FMX.ListBox.TListBoxItem.ItemData.

Access it in runtime via:

aPostCodeString := ListBoxItem.ItemData.Detail; // When reading
ListBoxItem.ItemData.Detail := aPostCodeString; // When writing

When adding an item to the listbox, take a reference to the item at the same time:

var
  ListBoxItem : TListboxItem;
...
ListBoxItem := ListBox.Items.Add(someText);
ListBoxItem.ItemData.Detail := aPostCodeString;

Upvotes: 2

Related Questions