10

Is it possible to do LiveBinding between controls, i.e. take 2 edit boxes and get the result of adding their contents together into a label. I'm sure it is, I just don't know where to start

Thanks

mmmm
  • 2,431
  • 2
  • 35
  • 56

3 Answers3

9

Have a look at the samples. SVN repository URL: https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE2/LiveBindings

An example:

----- Unit1.dfm -----

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 286
  ClientWidth = 426
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 62
    Width = 48
    Height = 13
    Caption = 'Edit1Edit2'
  end
  object Edit1: TEdit
    Left = 8
    Top = 8
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
    OnChange = EditChange
  end
  object Edit2: TEdit
    Left = 8
    Top = 35
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'Edit2'
    OnChange = EditChange
  end
  object BindingsList1: TBindingsList
    Methods = <>
    OutputConverters = <>
    UseAppManager = True
    Left = 20
    Top = 5
    object BindExpressionLabel11: TBindExpression
      Category = 'Binding Expressions'
      ControlComponent = Label1
      SourceComponent = BindScope1
      SourceExpression = 'Edit1.Text + Edit2.Text'
      ControlExpression = 'Caption'
      NotifyOutputs = False
      Direction = dirSourceToControl
    end
  end
  object BindScope1: TBindScope
    Left = 192
    Top = 16
  end
end

----- Unit1.pas -----

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Bind.EngExt, Vcl.Bind.DBEngExt,
  System.Rtti, System.Bindings.Outputs, Vcl.Bind.Editors, Data.Bind.Components,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    BindingsList1: TBindingsList;
    BindExpressionLabel11: TBindExpression;
    BindScope1: TBindScope;
    procedure EditChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  System.Bindings.Helper;

procedure TForm1.EditChange(Sender: TObject);
begin
  TBindings.Notify(Sender, 'Text');
end;

end.

How to use the IDE designer to produce the result:

  • put two edits (Edit1, Edit2), a label (Label1) and a TBindScope (BindScope1) on your form (Form1).
  • create an event handler for both edits' OnChange event (EditChange).
  • select Label1, expand the drop-down of LiveBindings property, select 'New Live Binding...', select TBindExpression
  • edit properties of the newly created BindExpressionLabel11: assign Caption to ControlExpression, BindScope1 to SourceComponent, Edit1.Text + Edit2.Text to SourceExpression
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • 2
    That looks pretty awesome. How is `SourceExpression = 'Edit1.Text + Edit2.Text'` compiled, evaluated or whatever? – David Heffernan Sep 08 '11 at 16:21
  • 3
    @David: there's a whole runtime (extensible) expression evaluation engine involved. – Ondrej Kelle Sep 08 '11 at 16:26
  • @OndrejKelle: I could not find TBindScope on the Delphi 10.4 palette, although TCustomBindScope implemented in Data.Bind.Components.pas -> any thoughts? – Didier Cabalé Jul 24 '20 at 12:05
  • @DidierCabalé IIRC it's not on the palette, it gets created by the live bindings editor. – Ondrej Kelle Jul 24 '20 at 14:31
  • @OndrejKelle: when you say "put two edits (Edit1, Edit2), a label (Label1) and a TBindScope (BindScope1) on your form (Form1).", can you please explain how you add the TBindScope? Note I did not find the way to add it neither from the IDE form Designer, nor from the LiveBindings Designer – Didier Cabalé Jul 25 '20 at 10:31
  • Sorry - I don't remember and I don't have XE2 anymore. – Ondrej Kelle Jul 25 '20 at 15:33
  • After some RAD Studio versions where TBindScope had disappeared, it comes again with RAD Studio version 10.4.1.. – Didier Cabalé Sep 06 '20 at 12:45
4

The sample project at the (Default) location of:

C:\Users\Public\Documents\RAD Studio\9.0\Samples\Delphi\LiveBinding\Components\bindexpression\fmx\BindExpressionSampleProject.dproj

does precisely that.

Nick Hodges
  • 16,902
  • 11
  • 68
  • 130
  • Not exactly, it shows a simple binding between one source and one control component. To get two distinct source components involved in a single expression, I believe you need the TBindScope component to resolve Edit1 and Edit2 references. You could also assign Form1 to SourceComponent but that's using the global variable Form1 with all the consequences. – Ondrej Kelle Sep 08 '11 at 16:47
  • Note that these demos (the LiveBindings ones) are only available via the ESD install. The ISO image doesn't contain them; you have to go to the link TOndrej supplied in his answer if you installed from the ISO. – Ken White Sep 08 '11 at 17:59
  • @KenWhite --The demos are also downloadable via the Subversion link: http://sourceforge.net/projects/radstudiodemos/ – Nick Hodges Sep 08 '11 at 18:32
  • @Nick: "This project has no files" from the link you provided. There is a link to the svn page from there, which is the same link TOndrej posted. Am I missing something? – Ken White Sep 08 '11 at 19:06
-1

You don't need to TBindScope to bind components together. Say you have edit1 and edit2 on the form. If you set edit2 BindingSource to edit1 it will be link to changes to edit1

  • He was asking about adding the properties of 2 components together. Your answer seem to be about assigning the property of one component to another. – chuacw Feb 07 '14 at 05:38