c# - Issue with moving items between listboxes -
c# - Issue with moving items between listboxes -
i have 2 listboxes in windows c# winforms application. have written code move items between 2 listboxes. can select any/multiple items , move them between boxes. if select bottom item in listbox , move other listbox, content in table moved shows incorrectly. instead of showing actual contents, it's showing:- programname__.objectname each entry above entry moved! happens lastly item in listbox. if select entry shows wrong , move other listbox, right info shows up.
this appears me bug, i'm not sure. can move other item in listbox , both listboxes show properly.
public class customers { public int id { get; set; } public string name {get; set; } } this.table1box.datasource = this.customersbindingsource; this.table1box.displaymember = "name"; this.table1box.name = "table1name"; this.table1box.selectionmode = system.windows.forms.selectionmode.multiextended; this.table1box.sorted = true; this.table1box.tabindex = 13; this.table1box.valuemember = "id"; this.table2box.datasource = this.customersbindingsource; this.table2box.displaymember = "name"; this.table2box.name = "table2name"; this.table2box.selectionmode = system.windows.forms.selectionmode.multiextended; this.table2box.sorted = true; this.table2box.tabindex = 14; this.table2box.valuemember = "id"; // click method moving table 1 -> table 2 private void table1totable2button_click(object sender, eventargs e) { foreach (int in this.table1box.selectedindices) { selectedcustomer = (customers)this.table1box.items[i]; table2list.add(selectedcustomer); table1list.remove(selectedcustomer); } this.table1box.datasource = this.emptylist; this.table1box.datasource = this.table1list; this.table1box.update(); this.table2box.datasource = this.emptylist; this.table2box.datasource = this.table2list; this.table2box.update(); }
![picture @ start of program] http://www.mclenaghan.com/pic1.jpg
![picture after moving lastly item] http://www.mclenaghan.com/pic2.jpg
![picture moving item 2] http://www.mclenaghan.com/pic3.jpg
make sure binding bindinglist<customers>
.
you don't need set datasource
empty list , set actual list 1 time again , phone call listbox.update()
. seems working means doing wrong in binding.
one more thing- not edit designer-generated code hand, utilize properties pane. find if alter code sequence in initializecomponent
method, listbox can display incorrectly.
bindinglist<customers> table1list; bindinglist<customers> table2list; public formwith2listboxes() { initializecomponent(); table1list = new bindinglist<customers>(); table1list.add(new customers() { id = 1, name = "name1" }); table1list.add(new customers() { id = 2, name = "name2" }); table1list.add(new customers() { id = 3, name = "name3" }); table2list = new bindinglist<customers>(); table2list.add(new customers() { id = 4, name = "name4" }); this.table1box.datasource = this.table1list; this.table2box.datasource = this.table2list; } private void table1totable2button_click(object sender, eventargs e) { foreach (int in this.table1box.selectedindices) { var selectedcustomer = (customers)this.table1box.items[i]; table2list.add(selectedcustomer); table1list.remove(selectedcustomer); } }
c# winforms listbox
Comments
Post a Comment