LiveBindings Tutorial

From RAD Studio
Jump to: navigation, search


Go Up to LiveBindings in RAD Studio

This tutorial guides you through the steps of creating a track spinner, using LiveBindings.

Step 1: Creating the Project

1. Create a new project: File > New >Multi-Device Application
In the wizard, choose Blank Application.
2. Select the form and change the Caption property in the Object Inspector to "LiveBindings Sample".
3. Add a TEdit component to the form, and change the KeyboardType property to NumberPad.
4. Add a TLabel component to the form, and change the Text property to "Value".
5. In the Tool Pallete, locate TTrackBar component drop it onto the form.

FormTrack.png

Step 2: Adding Fields

1. In the Tool Palette, locate a TPrototypeBindSource component and drop it onto the form.
2. Right-click the PrototypeBindSource1 component and select Add Field...
3. From Add Field dialog box, select ftInteger and press OK.

AddField.png

Step 3: Creating LiveBindings

Open the LiveBindings Designer, View > LiveBindings Designer, and drag the Text property of the TEdit onto the Field1 property of the PrototypeBindSource1 and also drag the Value property of the TTrackBar onto the Field1 property to bind these properties.

LBD.png

Step 4: Creating Track Spinner Programatically

1. Add a TLine component to the form, and then in the Object Inspector, set the LineType property to Top and the Align property to Center.
2. Add a TEdit component to the form, and change the KeyboardType property to NumberPad.
3. Add a TLabel component to the form, and change the Text property to "Value".
4. In the Tool Pallete, locate TTrackBar component drop it onto the form.
5. Add a TPrototypeBindSource to the form.
6. In the Tool Palette, locate a TBindingsList component and drop it onto the form.

FormTrackComplete.png

To add the OnCreate event handler

  • LinkControlToFieldEdit and LinkControlToFieldTrack are member variables declared in the private section of TForm1 class.

Delphi

  private
    { Private declarations }
    LinkControlToFieldEdit: TLinkControlToField;
    LinkControlToFieldTrack: TLinkControlToField;

C++

private: // User declarations
	TLinkControlToField *LinkControlToFieldEdit;
	TLinkControlToField *LinkControlToFieldTrack;
  • In the public section on TFrom1 class, add these procedures:

Delphi

  public
    { Public declarations }
    procedure LinkControlToComponent(LinkControl: TLinkControlToField;
      Component: TComponent; FieldName: String);
    procedure AddFieldToPrototypeBindSource(FieldType: TGeneratorFieldType;
      Name: String);
  • Implementation for procedures above:

Delphi

procedure TForm3.LinkControlToComponent(LinkControl: TLinkControlToField;
  Component: TComponent; FieldName: String);
begin
  LinkControl.Category := 'Qiuck Bindings';
  LinkControl.DataSource := PrototypeBindSource2;
  LinkControl.FieldName := FieldName;
  LinkControl.Control := Component;
  LinkControl.Track := False;
  LinkControl.Active := True;
end;

procedure TForm3.AddFieldToPrototypeBindSource(FieldType: TGeneratorFieldType;
  Name: String);
begin
  PrototypeBindSource2.FieldDefs.AddFieldDef;

  PrototypeBindSource2.AutoActivate := True;
  PrototypeBindSource2.AutoPost := False;
  PrototypeBindSource2.FieldDefs.Items[0].Name := Name;
  PrototypeBindSource2.FieldDefs.Items[0].FieldType := FieldType;
  PrototypeBindSource2.FieldDefs.Items[0].ReadOnly := False;
end;
1. Select form Structure Form1 component.
2. In the Object Inspector, open the Events tab, an then double-click OnCreate.
3. In the Code Editor, add the following code:

Delphi:

procedure TForm3.FormShow(Sender: TObject);
begin
  AddFieldToPrototypeBindSource(TGeneratorFieldType.ftInteger, 'Field1');

  // Create the links
  LinkControlToFieldEdit := TLinkControlToField.Create(BindingsList2);
  LinkControlToFieldTrack := TLinkControlToField.Create(BindingsList2);

  LinkControlToComponent(LinkControlToFieldEdit, Edit2, 'Field1');
  LinkControlToComponent(LinkControlToFieldTrack, TrackBar2, 'Field1');

end;

C++:

void __fastcall TForm1::FormShow(TObject *Sender)
{
	PrototypeBindSource2->FieldDefs->AddFieldDef();

	PrototypeBindSource2->AutoActivate = true;
	PrototypeBindSource2->AutoPost = false;
	PrototypeBindSource2->FieldDefs->Items[0]->Name = "Field1";
	PrototypeBindSource2->FieldDefs->Items[0]->FieldType = TGeneratorFieldType::ftInteger;
	PrototypeBindSource2->FieldDefs->Items[0]->ReadOnly = false;

	LinkControlToFieldEdit = new TLinkControlToField(BindingsList2);
	LinkControlToFieldTrack = new TLinkControlToField(BindingsList2);

	LinkControlToFieldEdit->Category = "Quick Bindings";
	LinkControlToFieldEdit->DataSource = PrototypeBindSource2;
	LinkControlToFieldEdit->FieldName = "Field1";
	LinkControlToFieldEdit->Control = Edit2;
	LinkControlToFieldEdit->Track = false;
	LinkControlToFieldEdit->Active = true;

	LinkControlToFieldTrack->Category = "Quick Bindings";
	LinkControlToFieldTrack->DataSource = PrototypeBindSource2;
	LinkControlToFieldTrack->FieldName = "Field1";
	LinkControlToFieldTrack->Control = TrackBar2;
	LinkControlToFieldTrack->Track = false;
	LinkControlToFieldTrack->Active = true;
}

The Results

To run the application, press F9 or choose Run > Run.

See Also