c# - How to sync selection index between two ListBoxes -
i have 2 listboxes: listbox1, listbox2.
if select item in first listbox1, item of same index must automatically selected in listbox2. so, if select item 1 in listbox1 then, item 1 selected automatically in listbox2 , on.
not: found examples not work.
private void listboxcontrol2_selectedindexchanged(object sender, eventargs e) { listboxcontrol5.selectedindex = listboxcontrol2.selectedindex; }
edit:
i solved using selected index code in this answer in selectedvaluechanged event.
private void listboxcontrol2_selectedvaluechanged(object sender, eventargs e) { listboxcontrol5.selectedindex = listboxcontrol2.selectedindex; }
here's sample may want explore more, try add listbox
to form (in sample 3 listboxes) should following:
and here's source select same index whenever click on item on it:
public partial class form1 : form { public form1() { initializecomponent(); initializelistboxes(); } private void initializelistboxes() { //populate listboxes listbox1.items.add("apple"); listbox1.items.add("orange"); listbox1.items.add("mango"); listbox2.items.add("milk"); listbox2.items.add("cheese"); listbox2.items.add("butter"); listbox3.items.add("coffee"); listbox3.items.add("cream"); listbox3.items.add("sugar"); //subscribe same events listbox1.selectedindexchanged += listbox_selectedindexchanged; listbox2.selectedindexchanged += listbox_selectedindexchanged; listbox3.selectedindexchanged += listbox_selectedindexchanged; } void listbox_selectedindexchanged(object sender, eventargs e) { listbox listbox = (listbox)sender; listbox1.selectedindex = listbox.selectedindex; listbox2.selectedindex = listbox.selectedindex; listbox3.selectedindex = listbox.selectedindex; } }
what happens on initializelistboxes
subscribe same event trigger selectedindexchanged
event, , select appropriate item each of listbox
.
Comments
Post a Comment