Solved

C# Listbox help

Posted on 2010-09-04
10
243 Views
Last Modified: 2012-08-14
Hi,

Im creating a till system with c#, which im not amazingly familar with.  What i want to happen with a push of a button is the name and a $ value to be sent to the list box wth the total $ value to be added up in a seperate box, I would also like to be able to delete the line out of the list box and the value to be removed from the 'running total'

I cant seem to figure out how to use values with ListBox, or if there would be a better system to use for this.  Any help would be greatly appreciated.

Thanks
0
Comment
Question by:grazhull
  • 7
  • 3
10 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33603236
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33603259
how are you wanting to populate the listbox
0
 

Author Comment

by:grazhull
ID: 33603275
Just line by line, like you would see on well a till :P so far ive just added to it with

 TillDisplay.Items.Add(button1.Text + itemprice[1]);

but when i delete the line            

 TillDisplay.Items.Remove(TillDisplay.SelectedItem);

my total wont get updated, because i add it up seperatly which is why im trying to work out how to assign a value to the line im adding then i can just add up the value of each line and if a line gets removed that value will be taken away!?
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 33603327
Not sure if the standard listbox can do that properly for you. You might need to use ListView
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33603369
Ive been looking here which contains samples for various things http://msdn.microsoft.com/en-us/library/6dwb14tw.aspx
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 500 total points
ID: 33603384
ok, step 1. Adding items. If you had a listview called myListView you can define the columns to use in code as well as populating them

So here I am creating two columns and giving it a title

            // Add columns stating title, width and alignment
            myListView.View = View.Details;
            myListView.Columns.Add("Fruit", 60, HorizontalAlignment.Left);
            myListView.Columns.Add("Price", 40, HorizontalAlignment.Right);

I then populate by first adding a row. This sets the text property so the text here is what you see in the first col.
But wait, we have a 2nd column so we use SubItems to add to the subsequent columns. You have to specify the index of the item so for simple add's i.e. appending list, I just use the count of items

            // Populate with data
            myListView.Items.Add("Apples");
            myListView.Items[myListView.Items.Count-1].SubItems.Add("20");

            myListView.Items.Add("Pears");
            myListView.Items[myListView.Items.Count - 1].SubItems.Add("30");

            myListView.Items.Add("Melon");
            myListView.Items[myListView.Items.Count - 1].SubItems.Add("100");
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 500 total points
ID: 33603430
For removal you iterate thru the list, find the selected one and process that row.

Here I am showing you just that. I find the selected row, display to the user the fruit and price then go and remove that row.

This works for multiselect, if you want just the one select, ensure you break out of the loop


            if (myListView.SelectedItems.Count > 0)
            {
                for (int i = 0; i < myListView.Items.Count; i++)
                {
                    if (myListView.Items[i].Selected == true)
                    {
                        string selectedFruit = myListView.Items[i].SubItems[0].Text;
                        string selectedPrice = myListView.Items[i].SubItems[1].Text;
                        MessageBox.Show("Selected Fruit is " + selectedFruit + " with a price of " + selectedPrice);
                        myListView.Items.Remove(myListView.Items[i]);
                    }
                }
            }
0
 

Author Comment

by:grazhull
ID: 33603441
with subitems.add it requires a string, is there anyway it can be made to use say a float?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33603493
use tostring eg
float a = 20
....add (  a.ToString());
0
 

Author Closing Comment

by:grazhull
ID: 33603551
Great Help! Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now