c# - How do I add to a ListBox using a method in another class? -
i feel i'm missing obvious here. this screenshot of form.
i have 2 classes, shoppingbasket , orderitem, , form1 class. have 4 properties in orderitem want use in shoppingbasket. want take product name in textbox1, quantity in numericupdown1, , latest price in textbox2, click add button validate values using orderitem class, put them addproduct method in shoppingbasket class add line listbox in form.
form1:
public partial class form1 : form { public form1() { initializecomponent(); } private void addbutton_click(object sender, eventargs e) { decimal latestprice; shoppingbasket addbuttonshoppingbasket = new shoppingbasket(); decimal.tryparse(textbox2.text, out latestprice); orderitem currentitemquantity1 = new orderitem(textbox1.text, latestprice, convert.toint32(numericupdown1.value)); addbuttonshoppingbasket.addproduct(currentitemquantity1.productname, currentitemquantity1.latestprice, currentitemquantity1.quantity); } } shoppingbasket:
public class shoppingbasket { public shoppingbasket() { } public void addproduct(string productname, decimal latestproductvalue, int quantity) { form1 newform = new form1(); string itemformatstring = "{0,-50}{1,0}{2,50}"; newform.listbox1.items.add(string.format(itemformatstring, productname, convert.tostring(quantity), convert.tostring(latestproductvalue))); } } orderitem:
public class orderitem { public orderitem(string productname, decimal latestprice, int quantity) { productname = productname; latestprice = latestprice; quantity = quantity; totalorder = latestprice * quantity; } public string productname { get; set; } public decimal latestprice { get; set; } public int quantity { get; set; } public decimal totalorder { get; set; } }
your problem is, creating new form shoppingbasked, whenever product added:
public void addproduct(string productname, decimal latestproductvalue, int quantity) { form1 newform = new form1(); string itemformatstring = "{0,-50}{1,0}{2,50}"; newform.listbox1.items.add(string.format(itemformatstring, productname, convert.tostring(quantity), convert.tostring(latestproductvalue))); } newform not form called addproduct! if not see newform anywhere (because newform.show() isn't called), list item get's added "invisible" form, not original one.
to solve problem, suggest passing form parameter addproduct:
public void addproduct(form1 form, string productname, decimal latestproductvalue, int quantity) { string itemformatstring = "{0,-50}{1,0}{2,50}"; form.listbox1.items.add(string.format(itemformatstring, productname, convert.tostring(quantity), convert.tostring(latestproductvalue))); } and call this:
private void addbutton_click(object sender, eventargs e) { // ... // current code here // ... addbuttonshoppingbasket.addproduct(this, currentitemquantity1.productname, currentitemquantity1.latestprice, currentitemquantity1.quantity); } also general advice continue changing design. @ moment, shoppingbasket highly coupled form1 - means, cannot add new items shopping basket other source form1! shoppingbasket should not care source of item receives. @ moment create new shoppingbasket, each time insert item. means, can have 1 item per shoppingbasket. further learning, suggest following points:
- make
shoppingbasketmember variable ofform1. - when adding items, add item member variable.
- do not pass form
addproduct, instead makeshoppingbasketprovide information items contains. - call
listbox1.items.addright afteraddproduct.
then shoppingbasket not care how products presented, cares how products stored internally.
Comments
Post a Comment