2
votes

How can I sync two TListBox objects, by accepting any change on any of the listboxes to be available in both the listboxes.

===================== updated ==================================================== this question is updated after the first answer

if we have to use

bindingslist1.Notify(ListBox2,''); 

all the time doing a change to the list box ,what is the purpose of having livebibnding instead of using

ListBox1.Items.Assign(ListBox2.Items ); 

all the time a change is happened.

1
Please notify me if i am going to use live-binding in a wrong scenarioVibeeshanRC

1 Answers

2
votes

In short this is how i did this as a firemonkey application, same should work with vcl i guess:

Create a new firemonkey HD application

Add two listboxes Add an edit and a button (to enter data)

Listbox1 -> Livebindings -> New Livebinding -> TBindExpression

A new component by the name of BindingsList1 is automatically added to the form

Edit the new bindexpression properties (BindExpressionListBox11 for me)

Direction = DirBiDirectional
Managed = true
NotifyOutputs = true
ControlExpression = Items
SourceExpression = Items

Create a buttonclick event (or you can put it in onChanged but that for some reason doesn't get called when adding an item. It does get called if you select something on the list):

procedure TForm1.Button1Click(Sender: TObject);
begin
   listbox2.Items.Add(edit1.Text);
   bindingslist1.Notify(listbox2, '');
end;

Now whenever you add an item to listbox2 the change is reflected to listbox1 aswell and vice versa (due to the dirBiDirectional setting). If adding to listbox1 instead, you need to call:

bindingslist1.Notify(listbox1, '');

Hope this helps getting you started.

Embarcadero also has some good livebinding stuff @ http://docwiki.embarcadero.com/RADStudio/en/LiveBindings_in_RAD_Studio