2
votes

There is a ComboBox on the FMX Form. It is binded with a datasource (table that has an id-integer and speciality - varchar fields) in the following manner-

object LinkFillControlToField1: TLinkFillControlToField
      Category = 'Quick Bindings'
      Control = ComboBox1
      Track = True
      FillDataSource = BindSourceDB1
      FillValueFieldName = 'id'
      FillDisplayFieldName = 'speciality'
      AutoFill = True
      BufferCount = -1
      AutoBufferCount = False
      FillExpressions = <>
      FillHeaderExpressions = <>
      FillBreakGroups = <>
    end

It is simple to get access to the value of chosen speciality (from ComboBox1.Selected.Text) but I can not find a way to access the id value of the selected item without extra SQL requests. Where is it stored in TComboBox or its ListBox? Where is SelectedValue stored and how to get it (as integer)?

4
A very similar question (but for VCL) is posted hereyonojoy

4 Answers

3
votes

tm. You must set the livebinding link between the SelectedValue of the combo with an other control. I have attached a screeshoot of the binding editor. The label will show the id. enter image description here

2
votes

You can access the id value of the selected item by the TLinkFillControl that defines the binding:

procedure TForm1.ComboBox1Change(Sender: TObject);
var
    Id: Integer;
begin
    if TryStrToInt(LinkFillControlToField1.BindList.GetSelectedValue.AsString, Id) then
      ShowMessage(IntToStr(Id));
end;

If Item.LookupData is bound, BindList.GetSelectedValue delivers the corresponding bound data. If I remember rightly Delphi stores the value internally in a dictionary.

1
votes

I am currently using the following way to resolve the issue. I handle OnFillingListItem event in the following way and store id number in ComboBox Items. I use Tag property though it is not actually good.

procedure TForm1.LinkFillControlToField1FillingListItem(Sender: TObject;
  const AEditor: IBindListEditorItem);
begin
  (AEditor.CurrentObject as TListBoxItem).Tag :=
    YourLookuptable.FieldByName('id').AsInteger;
end;

Later on I fetch the Item id from ListBox1.Selected.Tag. This gives me a reliable ID.

0
votes

ComboBox1.ItemIndex is all you need.

To get the text associated with the selected item you can do the following:

Text := ComboBox1.Items[ ComboBox1.ItemIndex ];

See: http://docwiki.embarcadero.com/Libraries/Sydney/en/FMX.ListBox.TCustomComboBox.ItemIndex