如何从代码获取ComboBox的SelectedValue? [英] How do I get the SelectedValue of a ComboBox from code?

查看:807
本文介绍了如何从代码获取ComboBox的SelectedValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LiveBindings来构建一个类似于 TLookupComboBox 的东西。



我已经放置了一个普通的 TComboBox 在VCL表单上。我还有一些数据集,其中包含两个字段 id text



然后我使用LiveBindings编辑器创建一个 TBindSourceDB 和一个 TBindingsList 。 / p>

只有一个绑定:

 对象BindingsList1:TBindingsList 
方法=<>
OutputConverters =<>
UseAppManager = True
Left = 244
顶部= 229
对象LinkFillControlToField1:TLinkFillControlToField
类别='快速绑定'
Control = ComboBox1
Track = True
FillDataSource = BindSourceDB1
FillValueFieldName ='id'
FillDisplayFieldName ='text'
AutoFill = True
BufferCount = -1
FillExpressions =< ;>
end
end

正如你所看到的,我有不同的 FillValueFieldName FillDisplayFieldName



LiveBindings设计器如下所示:





ComboBox中填充了文本的值,所以我认为我正确设置。



如何从代码中获取 SelectedValue



我可以直观地绑定值为 TLabel ,然后我可以读取 TLabel.Caption ,但肯定有一个更简单的方法? / p>

PS:我不想将值存储在数据库中,否则我只需使用 TDBLookupComboBox 。 / p>

编辑:我尝试使用一个 TPrototypeBindSource ,但这没有明显的方法从代码访问字段。我还试图使用第二个 TBindSourceDB 以及一个 TClientDataSet ,但这感觉就像使用大锤破解一个坚果。

解决方案

您可以通过相应的 TLinkFillControlToField

  procedure TForm1.ComboBox1Change(Sender:TObject); 
var
SelectedValue:Integer;
begin
如果TryStrToInt(LinkFillControlToField1.BindList.GetSelectedValue.AsString,SelectedValue)然后
DoSomethingWith(SelectedValue);
结束


I am trying to build something like a TLookupComboBox using LiveBindings.

I have placed a normal TComboBox on a VCL form. I also have a data set with some rows that have the two fields id and text.

Then I used the LiveBindings editor to create a TBindSourceDB and a TBindingsList.

There is just one binding in it:

object BindingsList1: TBindingsList
  Methods = <>
  OutputConverters = <>
  UseAppManager = True
  Left = 244
  Top = 229
  object LinkFillControlToField1: TLinkFillControlToField
    Category = 'Quick Bindings'
    Control = ComboBox1
    Track = True
    FillDataSource = BindSourceDB1
    FillValueFieldName = 'id'
    FillDisplayFieldName = 'text'
    AutoFill = True
    BufferCount = -1
    FillExpressions = <>
  end
end

As you can see I have different fields for FillValueFieldName and FillDisplayFieldName.

The LiveBindings designer looks like this:

The ComboBox is filled with the values from the field text, so I think I set it up correctly.

How can I get the SelectedValue from code?

I could visually bind the value to a TLabel and then I could read the TLabel.Caption, but surely there is an easier way?

PS: I do not want to store the value in the DB, otherwise I would just use a TDBLookupComboBox.

Edit: I have tried to use a TPrototypeBindSource, but that has no obvious way to access the fields from code. I have also tried to use a second TBindSourceDB together with a TClientDataSet which works, but that feels like using a sledgehammer to crack a nut.

解决方案

You can access the selected value via the corresponding TLinkFillControlToField:

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

这篇关于如何从代码获取ComboBox的SelectedValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆