Listbox

how to carry out calculations on a listbox

Page 1 of 1

4 Replies - 829 Views - Last Post: 12 November 2010 - 06:11 AM Rate Topic: -----

#1 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Listbox

Posted 11 November 2010 - 04:43 PM

I know this is probably simple for a lot of people but I just cant seem to figure it out and i have been looking up the msdn website. . .not getting very far and i am new to this.

My question, I have a listbox that has 10 names of DVDs and the user can select one or more dvds and when they click the total button it should calculate the total cost of purchase depending on the amount of dvds selected from the list (by holding in the ctrl key).

so i thought the code was somthing like this


     double total = DVDs.SelectedItem * 2.50;
     //then output to user



although this doesnt work SelectedIndex does but it wont calculate it correct for obvious reasons

Just need to be put on the right track, Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Listbox

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Listbox

Posted 11 November 2010 - 04:58 PM

selectedItem returns a string, need to convert it to a double before multiplying

or maybe that's selected value, try selectedItem.Text if the first don't work

Edit: after double checking, and re-reading what you were trying to do, you need get the count of the items selected

something like this should work
ListBox lb = new ListBox();
int count = 0;
	
foreach (ListItem item in lb.items)
{
	if (item.Selected) count++;
}


This post has been edited by Nakor: 11 November 2010 - 05:04 PM

Was This Post Helpful? 1
  • +
  • -

#3 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Listbox

Posted 12 November 2010 - 03:30 AM

This is what I got quickly... I had a listbox called listBox1 and a button called button1

private void button1_Click(object sender, EventArgs e)
{
    // initialise total to zero
    double total = 0;

    // are there selected items?
    if (listBox1.SelectedItems.Count > 0)
    {
        // calculate total
        total = listBox1.SelectedItems.Count * 2.50;
    }

    // output example
    this.Text = "" + total.ToString();
}



hope this helps :)
Was This Post Helpful? 1
  • +
  • -

#4 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Listbox

Posted 12 November 2010 - 04:34 AM

Hope this help but please try to learn this don't just use it.

namespace DVD
{
    public partial class frmDVD : Form
    {
        public frmDVD()
        {
            InitializeComponent();
            IBXDvD.SelectionMode = SelectionMode.MultiExtended;
            IBXDvD.Items.Add("Rambo");
            IBXDvD.Items.Add("Saw 3D");
            IBXDvD.Items.Add("Hurt Locker");
            IBXDvD.Items.Add("Road Trip");
            IBXDvD.Items.Add("Leon");
            IBXDvD.Items.Add("Iron Eagle");
            IBXDvD.Items.Add("Goodfellas");
            IBXDvD.Items.Add("Matrix");
        }

        private void btnRent_Click(object sender, EventArgs e)
        {
            double selections,dvds;
            selections = IBXDvD.SelectedItems.Count;
            dvds= selections * 2.50;
            lblPrice.Text=("The Price for the dvds you have selected is " +"" + dvds.ToString("##0.00"));
        }

    }
}

Was This Post Helpful? 1
  • +
  • -

#5 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Listbox

Posted 12 November 2010 - 06:11 AM

Thanks everyone for your help i was thinking i would need a for loop or if statement. I understand your code m_wylie85 for your button but dont understand why you would write the code to add the items to the listbox when you can do it in the properties window, however i liked your code as it is something different to what i have learnt in other programing languages and it works!

batesy3k I understood your code very well as it is something similar to what I have learnt. I decided to use it and part of m_wylie85!! Nakor yours was helpful too altough im not sure of the difference in for each and for loops.

I guess i was making this harder than what it was.

Thanks!!!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1