C# add list of strings to combobox and when item is remove automatic affect to list -
i want add strings list combobox, , when 1 of item removed ui, want automatically effect on list (that list remove selected string).
what best tehnic kind of problems.
example:
list<string> users = new list<string>(){ "frsuser", "secuser", "thruser", "fouuser" }; private void frmmain_load(object sender, eventargs e) { foreach(var user in users) cmbuser.items.add(user); } private void btnremove_click(object sender, eventargs e) { cmbuser.items.removeat(cmbuser.selectedindex); // should removed here? users.removeat(cmbuser.selectedindex); }
this done using bindingsource
handles interaction between collection of combobox items , list
private void frmmain_load(object sender, eventargs e) { bindingsource bs = new bindingsource(); bs.datasource = users; c.datasource = bs; }
now @ button click event use code
private void btnremove_click(object sender, eventargs e) { if(c.selectedindex == -1) return; bindingsource bs = c.datasource bindingsource; bs.removeat(c.selectedindex); // show updated list foreach(string u in users) console.writeline(u); }
Comments
Post a Comment