Solved

C# Listbox help

Posted on 2010-09-04
10
248 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 
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

Independent Software Vendors: 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

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…
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
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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