Solved

C# Listbox help

Posted on 2010-09-04
10
242 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
Comment Utility
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
how are you wanting to populate the listbox
0
 

Author Comment

by:grazhull
Comment Utility
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
Comment Utility
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
Comment Utility
Ive been looking here which contains samples for various things http://msdn.microsoft.com/en-us/library/6dwb14tw.aspx
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
use tostring eg
float a = 20
....add (  a.ToString());
0
 

Author Closing Comment

by:grazhull
Comment Utility
Great Help! Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

12 Experts available now in Live!

Get 1:1 Help Now