c# - Displaying the value of a property from a class according to selected item in listbox -


i made class named product , added different products class in main program this:

product1 = new product(); product1.name = "product1"; product1.price = 3.50;  product product2 = new product(); product2.name = "product2"; product2.price = 4; 

and i've got listbox fill method:

private void fillproducts(string item)         {             lstproducts.items.add(item);         } 

so when use method this: fillproducts(product1.name);

now want achieve when press button (btnconfirm) see product has been selected in listbox , price of product , display in label

lblconfirm.text = "the price of product1 is: " + *the price of product1*; 

so need display price of product1 in label , don't want use if-statements every single product because there on 200 if-statements. if unclear in question please tell me.

just populate listbox product, not string:

private void fillproducts(product item) {     lstproducts.items.add(item); } 

use properties built-in listbox tell value display:

lstproducts.displaymember = "name"; 

then access selecteditem property selected item when need it:

var price = ((product)lstproducts.selecteditem).price  lblconfirm.text = "the price of product1 is: " + price; 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -