Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi!

I've got a listbox with items to be rented, what i would like to do is to get the price for each item out of the listbox to display the total price in a textbox.

how do i get the values out of the listbox, have tried loads of different ways and can't make it work.

the strings in the list box looks like this:

Ford 2010 $130 2011-05-18 2011-05-20

not sure if it's any help but I really really do need som help!
Thanks!!
Posted
Comments
Om Prakash Pant 18-May-11 8:46am    
your question is not clear. can you add some code and more details?

1 solution

The problem lies with the fact that the ListBox items are stored as strings. Ideally you should create a class for your rentable items something like this

C#
class ItemForRent
    {
        public string Description;
        public DateTime Year;
        public decimal Price;
        public DateTime AvailableFrom;
        public DateTime AvailableTo;
        public static List<ItemForRent> rentableItems()
        {
            //retrieve items from data source
            return new List<ItemForRent>();
        }
    }


Then you could have a List<itemforrent> as a form level variable, which you can bind to your ListBox like this:

C#
List<ItemForRent> rentableThings;
       public Form1()
       {
           InitializeComponent();
           rentableThings = ItemForRent.rentableItems();
           listBox1.DataSource = rentableThings;
       }


Then to get Total Price all you need do is this

C#
public decimal GetTotalPrice()
        {
            return rentableThings.Sum(item => item.Price);
        }


I think this is a better way to solve your problems. Hope this helps
 
Share this answer
 
Comments
fjdiewornncalwe 18-May-11 8:58am    
+5. A reasonable solution. My question to the OP would be why he is using a listbox instead of a grid with checkboxes for this. Then he could simply use a specific column for the price.
Wayne Gaylard 18-May-11 9:05am    
Should we apply for his job?
fjdiewornncalwe 18-May-11 15:01pm    
No, because then we'll have to fix all the stuff that will be designed like this. :)
Kim Togo 18-May-11 9:06am    
Good answer, my 5.
Wayne Gaylard 18-May-11 9:16am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900