.net - How do I make a ListBox refresh its item text? -
i'm making example hasn't yet realized controls listbox
don't have contain strings; had been storing formatted strings , jumping through complicated parsing hoops data out of listbox
, i'd show him there's better way.
i noticed if have object stored in listbox
update value affects tostring
, listbox
not update itself. i've tried calling refresh
, update
on control, neither works. here's code of example i'm using, requires drag listbox , button onto form:
public class form1 protected overrides sub onload(byval e system.eventargs) mybase.onload(e) integer = 1 3 dim tempinfo new numberinfo() tempinfo.count = tempinfo.number = * 100 listbox1.items.add(tempinfo) next end sub private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click each objitem object in listbox1.items dim info numberinfo = directcast(objitem, numberinfo) info.count += 1 next end sub end class public class numberinfo public count integer public number integer public overrides function tostring() string return string.format("{0}, {1}", count, number) end function end class
i thought perhaps problem using fields , tried implementing inotifypropertychanged, had no effect. (the reason i'm using fields because it's example , don't feel adding few dozen lines have nothing topic i'm demonstrating.)
honestly i've never tried updating items in place before; in past i've been adding/removing items, not editing them. i've never noticed don't know how make work.
so missing?
bindinglist handles updating bindings itself.
using system; using system.componentmodel; using system.windows.forms; namespace testbindinglist { public class employee { public string name { get; set; } public int id { get; set; } } public partial class form1 : form { private bindinglist<employee> _employees; private listbox lstemployees; private textbox txtid; private textbox txtname; private button btnremove; public form1() { initializecomponent(); flowlayoutpanel layout = new flowlayoutpanel(); layout.dock = dockstyle.fill; controls.add(layout); lstemployees = new listbox(); layout.controls.add(lstemployees); txtid = new textbox(); layout.controls.add(txtid); txtname = new textbox(); layout.controls.add(txtname); btnremove = new button(); btnremove.click += btnremove_click; btnremove.text = "remove"; layout.controls.add(btnremove); load+=new eventhandler(form1_load); } private void form1_load(object sender, eventargs e) { _employees = new bindinglist<employee>(); (int = 0; < 10; i++) { _employees.add(new employee() { id = i, name = "employee " + i.tostring() }); } lstemployees.displaymember = "name"; lstemployees.datasource = _employees; txtid.databindings.add("text", _employees, "id"); txtname.databindings.add("text", _employees, "name"); } private void btnremove_click(object sender, eventargs e) { employee selectedemployee = (employee)lstemployees.selecteditem; if (selectedemployee != null) { _employees.remove(selectedemployee); } } } }
Comments
Post a Comment