Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Listbox help

Posted on 2010-09-04
10
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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