2

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.

Vibeeshan Mahadeva
  • 7,147
  • 8
  • 52
  • 102

1 Answers1

2

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

egmolio
  • 31
  • 1
  • 1
    thanks for the answer , but if i want to call Notify(listbox1, ''); always i do change , then what is the purpose of having live-bindings , instead of doing manually – Vibeeshan Mahadeva Dec 08 '11 at 10:49
  • According to the link in my post, using Managed=true and NotifyOutputs=true should do this automatically but it didn't seem to work for me atleast. As for the use the link suggests: ' Typically, the Sender parameter of the event handler is used to call BindingsList1.Notify(Sender, '');. In this way, you can use one event handler to handle notifications from many controls. ' – egmolio Dec 08 '11 at 11:06