Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Listbox help

Posted on 2010-09-04
10
Medium Priority
?
254 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 65

Accepted Solution

by:
rockiroads earned 2000 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
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 2000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline
Suggested Courses

927 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