How to dynamically add to a List<Object> in C# -
i have collection class class puts list<ingredient>
object, contents of ingredient object list displayed in checklistbox
using binding.
what i'm trying when user clicks on 1 of list items information in particular item copied list called list<pizza> mypizza
, display on list. reason want data go because once in list<pizza> mypizza
more things data.
i have tried doesn't seem work, below collectionclass
, windows form , ingredient class. didn't bother putting pizza class same ingredient class know how add listbox using item, want add list add list listbox.
i hope make sense, kind of new programming. thanks.
ingredient class.
public partial class form1 : form { //list<ingredient> myingredient = new list<ingredient>(); list<pizza> mypizza = new list<pizza>(); collectionclass mycollections = new collectionclass(); public form1() { initializecomponent(); mycollections.createlist(); checkedlistboxingredients.datasource = new bindingsource(mycollections.myingredient, null); checkedlistboxingredients.displaymember = "displayname"; } private void checkedlistboxingredients_itemcheck(object sender, itemcheckeventargs e) { foreach (pizza pizza in mypizza) { checkedlistboxingredients.items.add(pizza.toppingname); checkedlistboxingredients.items.add(pizza.unitname); checkedlistboxingredients.items.add(pizza.cost); checkedlistboxingredients.items.add(pizza.descriptionname); } if (e.newvalue == checkstate.checked) { yourpizza.datasource = new bindingsource(mypizza, null); yourpizza.displaymember = "displayname"; } } }
ingredient class.
public class ingredient { public string toppingname { get; set; } public string unitname { get; set; } public int cost { get; set; } public string descriptionname { get; set; } public ingredient(string pizzatoppingname, string pizzatoppingunit, int pizzacost, string pizzadescription) { toppingname = pizzatoppingname; unitname = pizzatoppingunit; cost = pizzacost; descriptionname = pizzadescription; } public string displayname { { return toppingname + " $"+cost; } } public override string tostring() { return toppingname; } }
collectionclass.
public class collectionclass { public list<ingredient> myingredient = new list<ingredient>(); public void createlist() { myingredient.add(new ingredient("cheese", "grams", 3, "grab cheese , sprinkle on top of pizza")); myingredient.add(new ingredient("olives", "grams", 2, "cut olives , sprickle on pizza")); myingredient.add(new ingredient("ham", "grams", 1, "spread cut ham on pizza")); myingredient.add(new ingredient("pineapple", "grams", 1, "spread cut chunks on pizza")); myingredient.add(new ingredient("pepperoni", "pieces", 1, "place slices on pepperoni on pizza")); myingredient.add(new ingredient("onion", "handfuls", 1, "sprinkle cut onion on pizza, try not cry")); myingredient.add(new ingredient("peppers", "grams", 1, "sprinkle cut peppers on pizza")); myingredient.add(new ingredient("anchovy", "grams", 1, "place on top of pizza")); myingredient.add(new ingredient("mushrooms", "grams", 1, "put gently on top of pizza")); } public void displaylist() { foreach (ingredient ingred in myingredient) if (ingred != null) { console.writeline("{0} {1} ${2}.00, {3}", ingred.toppingname, ingred.unitname, ingred.cost, ingred.descriptionname); } } }
Comments
Post a Comment