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

99封情书 提交于 2019-11-28 14:18:15

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.

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.

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.

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