winforms - Add items to combobox with multiple values C# -
currently have combobox 3 hard coded items. each item carries 2 values. i'm using switch case statement values each item depending on item selected.
switch(combobox.selectedindex) { case 0: // item 1 in combobox = 100; b = 0.1; break; case 1: // item 2 in combobox = 300; b = 0.5; break; //and on.... }
i'm trying add feature allow user add more items combobox inputted , b values. how able dynamically add case statements , define values under each case condition? i've had @ using datatable instead don't know how multiple valuemembers out of datatable when 1 item selected.
also, save user added items , it's corresponding values .dat file. when program re-opened able load list of items added user file. considered using streamwriter , readline i'm unsure how done.
you can use binding on combobox using datasource. combobox can bound other things primitive values (string/int/hardcoded values). make small class represents values setting in switch statement, , use displaymember property should visible in combobox.
an example of such basic class be
public class datastructure { public double { get; set; } public int b { get; set; } public string title { get; set; } }
since talking users adding values combobox dynamically, use bindinglist contains separate classes, bindinglist protected field inside class, add new datastructure when user adds one, , automatically updates combobox new value added.
the setup of combobox, can done in either form_load, or in form constructor (after initializecomponent() call), such:
// form public partial class form1 : form { // property contains items shown in combobox protected ilist<datastructure> dataitems = new bindinglist<datastructure>(); // way keep selected reference not have ask combobox, gets updated on selection changed events protected datastructure selecteddatastructure = null; public form1() { initializecomponent(); // create default values here dataitems.add(new datastructure { = 0.5, b = 100, title = "some value" }); dataitems.add(new datastructure { = 0.75, b = 100, title = "more value" }); dataitems.add(new datastructure { = 0.95, b = 100, title = "even more value" }); // assign dataitems combobox datasource combobox1.datasource = dataitems; // combobox should show in dropdown combobox1.displaymember = "title"; // set list only, no typing combobox1.dropdownstyle = comboboxstyle.dropdownlist; // register event triggers each time selection changes combobox1.selectedindexchanged += combobox1_selectedindexchanged; } // method add items dataitems (and automatically combobox bindingcontext) private void add(double a, int b, string title) { dataitems.add(new datastructure { = a, b = b, title = title }); } // when value changes, update selecteddatastructure field private void combobox1_selectedindexchanged(object sender, eventargs e) { combobox combo = sender combobox; if (combo == null) { return; } selecteddatastructure = combo.selecteditem datastructure; if (selecteddatastructure == null) { messagebox.show("you didn't select @ moment"); } else { messagebox.show(string.format("you selected {0} = {1:n2}, b = {2}", selecteddatastructure.title, selecteddatastructure.a, selecteddatastructure.b)); } } // add items on button click private void addcomboboxitembutton_click(object sender, eventargs e) { string title = textbox1.text; if (string.isnullorwhitespace(title)) { messagebox.show("a title required!"); return; } random random = new random(); double = random.nextdouble(); int b = random.next(); add(a, b, title); textbox1.text = string.empty; } }
like this, have selected item @ hand, can request values properties of selected, , don't have worry syncing combobox items visible
Comments
Post a Comment