How to get Item.LookupData and SelectedValue (as integer) of an FMX TComboBox at runtime?

别等时光非礼了梦想. 提交于 2019-11-27 08:13:33

问题


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


回答1:


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.




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/31453018/how-to-get-item-lookupdata-and-selectedvalue-as-integer-of-an-fmx-tcombobox-at

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!