2

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)?

asd-tm
  • 3,381
  • 2
  • 24
  • 41
  • A very similar question (but for VCL) is posted [here](http://stackoverflow.com/q/23712406/2306907) – yonojoy Apr 08 '16 at 09:44

4 Answers4

3

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

Pau Dominguez
  • 179
  • 1
  • 1
  • 8
  • Dear Pau Dominguez, thank you. I thought about this way. Hower it seems very unnatural to use a third (invisible) component only for the purpose of extracting the value that should have been stored in `TComboBox`. – asd-tm Jul 22 '15 at 20:12
  • I created a method (function) in TForm ascendant class that among the other actions was assigning the `id` value to `TCombobox.Tag`. `SelectedValue` was passed as a parameter. The function was called in BindingExpression. This is also not a good way. I wonder if we can get the property `SelectedValue` directly from code if it is accessible from `LiveBindings` engine. – asd-tm Jul 22 '15 at 20:22
  • 1
    You can link selectedvalue to the tag property of the same combobox and use it where You want. To do this press ... In the combo Bindings editor and add the tag property, then link selectedvalue to it. – Pau Dominguez Jul 23 '15 at 22:38
  • The designer restricts control and source components to be the same. And so we still have to use the third component. – asd-tm Jul 31 '15 at 08:05
  • Sorry. You can use the tag property of the label that names the combobox. The same design but link Selectedvalue to label1.tag – Pau Dominguez Aug 02 '15 at 22:32
2

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.

yonojoy
  • 5,486
  • 1
  • 31
  • 60
1

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.

asd-tm
  • 3,381
  • 2
  • 24
  • 41
  • If I remember correctly, the Delphi implementation to determine the `SelectedValue` by [`LinkFillControlToField1.BindList.GetSelectedValue`](http://stackoverflow.com/a/36496340/2306907) abuses the `Tag` property too. So mixing both solutions might not work. – yonojoy Apr 12 '16 at 07:02
0

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

Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27
phr
  • 1
  • 2
  • The `ComboBox1.ItemIndex` gives the id of the item of the `ComboBox` that is not the same as id of the record in the table. – asd-tm May 28 '21 at 08:18