gpt4 book ai didi

Delphi(FMX) 具有多个单选按钮的实时绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 03:42:34 24 4
gpt4 key购买 nike

我有一个包含 2 RadioButtons(具有相同 GroupName)的表单,我需要保存 'A'(如果选择了 RadioButton1)或 'I' (如果选择了 RadioButton2)在状态字段中使用 LiveBindings。

一个组件到一个字段很容易,但在本例中,我有两个组件从一个字段获取和设置值。

我创建了一个函数,该函数返回通过Groupname 选择的radiobutton 并手动填写字段,但我想要更自动化的东西。

提前致谢!

最佳答案

以下是实现此目的的步骤。

  1. 创建两个 RadioButton,将其命名为 RadioButton1RadioButton2
  2. 将两个单选按钮的 GroupName 属性设置为相同的字符串。
  3. 右键单击第一个单选按钮并选择“视觉绑定(bind)...”
  4. 在 LiveBindings 设计器中,右键单击单选按钮并选择 Bindable Members,然后选中复选框 IsChecked,然后单击“确定”按钮。
  5. 仍在实时绑定(bind)设计器中,现在在 IsChecked 属性和您要绑定(bind)的字段之间拖动一个链接(请注意,这可以是字符串字段)。
  6. 对另一个单选按钮重复步骤 4 和 5。

现在您已经快要崩溃了,但您需要将字符串转换为 bool 值,以便 IsChecked 属性将具有 bool 值。为此,请从 LiveBindings Designer 中为单选按钮选择绑定(bind)链接。然后在其 CustomFormat 属性中,分配以下字符串

IfThen(ToStr(%s)="Poor",True, False)

这将允许在基础数据库值为“差”时检查单选按钮

对其他单选按钮执行相同的操作,但使用不同的字符串

IfThen(ToStr(%s)="Excellent",True, False)

现在,为了让单选按钮能够更改底层数据库字段,您需要附加代码来执行此操作。让我们使用单选按钮的 OnClick 事件(附加到两个单选按钮)。此代码假设您的基础数据集名为 FDCustomer,并且您的字段名为 Status。请注意,事件发生时单选按钮尚未选中,因此我们查找 IsChecked 为 false。

if Sender = RadioButton1 then
begin
if not TRadioButton(Sender).IsChecked then // checking
begin
fdcustomer.Edit;
fdcustomer.FieldByName('Status').AsString:= 'Poor';
end;
end
else if Sender = RadioButton2 then
begin
if not TRadioButton(Sender).IsChecked then
begin
fdcustomer.Edit;
fdcustomer.FieldByName('Status').AsString:= 'Excellent';
end;
end;

关于Delphi(FMX) 具有多个单选按钮的实时绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35924916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com