C# listbox calculation using substring

adding items using substring

Page 1 of 1

7 Replies - 19430 Views - Last Post: 28 January 2010 - 04:41 AM Rate Topic: -----

#1 andylsbc6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-January 10

C# listbox calculation using substring

Post icon  Posted 15 January 2010 - 10:51 PM

hi, i am adding a calculation button's event handler in a listbox. In my form, there is 1 listbox which contains items like pencil - $3.20, pen - $1.20, etc. What i want to do is that i want to use a for statement to loop through all the items in the listbox. Convert each item from the listbox into string.Then use the string method "Substring" to extract the price of each item. Examples: pencil - $3.20 >>> but i only want 3.20 so that i can add them up and display it on a label. i have written code below but it just don't add up the items in the listbox and show the total....anyone can help please..any help will be much appreciated..thank you...^.^

private void btnCalculate_Click(object sender, EventArgs e)
	  {
		  int intItemLocation=0;
		  int intPriceBegin;
		  int intPriceEnd;
		  string strPrice;

		  strPrice = string.Format(lstStock.Text);
		  intItemLocation = strPrice.IndexOf(lstStock.Text.ToString());
		 

		  for (int i = 0; lstbox2.Items.Count >= i; i++)
		  {
			  intPriceBegin = strPrice.IndexOf("$", intItemLocation);

			  intPriceEnd = strPrice.IndexOf(" ", intPriceBegin);

			  strPrice = strPrice.Substring(intPriceBegin + 1, (intPriceEnd - intPriceBegin));

			  strPrice += ( 
			 String.Format("{0:C}",strPrice));

		  }	lblTotal.Text = strPrice;



Is This A Good Question/Topic? 0
  • +

#3 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C# listbox calculation using substring

Posted 15 January 2010 - 11:53 PM

Here is maybe an easier way:
{
	double amount = 0.0;
	double result;

	String[] items;

	ListBox.ObjectCollection contents = listBox2.Items;

	foreach (string str in contents)
	{
		items = str.Split(' ');

// assumes 3 fields: item name, a hypen, price in dollars
		Double.TryParse(items[2].Replace('$',' '), out result);

		amount += result;
	}
	MessageBox.Show(amount.ToString()); // I wanted to see the sum :)
}



The String Class has a spilt() method and a replace() method. See String Class

The Items property of the listbox is a ListBox.ObjectCollection. So you can copy the contents of a listbox and manipulate them. See ListBox.ObjectCollection Class
Was This Post Helpful? 1
  • +
  • -

#4 andylsbc6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-January 10

Re: C# listbox calculation using substring

Posted 16 January 2010 - 12:12 AM

ohh it works..thank you..=))
Was This Post Helpful? 0
  • +
  • -

#5 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# listbox calculation using substring

Posted 16 January 2010 - 03:16 AM

If I were you, I would bound objects to your listbox, so for every object in your listbox you would instantly know all its properties and you wouldn't have to parse the text of each listbox item. Check out my tutorial and feel free to post a question if anything unclear.

http://www.dreaminco...topic145733.htm


Hope this helps...
Was This Post Helpful? 1
  • +
  • -

#6 Dellfar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 10

Re: C# listbox calculation using substring

Posted 27 January 2010 - 02:36 AM

Attached Imagehi,

I make a programme similar to andylsbc6 programme but I have to listboxes one of them shows all items available and the other one shows the selected items from all items listbox. I use the code of n8wxs in calculate button to calculate the total but have a problem with one line. can help please?
I submit my all code of this code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Task2
{
	
	public partial class frmShopCalculator : Form
	{
	   
		public frmShopCalculator()
		{
			InitializeComponent();
		}

		private void lbAvailableItems_MouseDown(object sender, MouseEventArgs e)
		{
			lbAvailableItems.Sorted = true;
			if (lbAvailableItems.Items.Count == 0)
				return;

			int index = lbAvailableItems.IndexFromPoint(e.X, e.Y);
			string s = lbAvailableItems.Items[index].ToString();
			DragDropEffects DragDrop = DoDragDrop(s, DragDropEffects.All);

			if (DragDrop == DragDropEffects.All)
			{
				lbAvailableItems.Items.RemoveAt(lbAvailableItems.IndexFromPoint(e.X, e.Y));
			} 
		}

		private void lbMyCart_DragOver(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.All;
		}

		private void lbMyCart_DragDrop(object sender, DragEventArgs e)
		{
			lbMyCart.Sorted = true;
			if (e.Data.GetDataPresent(DataFormats.StringFormat))
			{
				string str = (string)e.Data.GetData(DataFormats.StringFormat);

				lbMyCart.Items.Add(str);
			}

		}

		private void btnCalculate_Click(object sender, EventArgs e)
		{
			double amount = 00.00;
			double result;

			String[] items;

			ListBox.ObjectCollection contents = lbMyCart.Items;

			foreach (string str in contents)
			{
				items = str.Split(' ');

				// assumes 3 fields: item name, a hypen, price in dollars
				Double.TryParse(items[2].Replace('$', ' '), out result);

				amount += result;
			}
			lblTotal.Text = amount.ToString(); 
		}
	
	}
}



This post has been edited by Dellfar: 27 January 2010 - 02:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: C# listbox calculation using substring

Posted 27 January 2010 - 02:48 AM

please paste code properly so we can read it easier and answer your question quicker :code:
Was This Post Helpful? 0
  • +
  • -

#8 Dellfar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 10

Re: C# listbox calculation using substring

Posted 27 January 2010 - 02:57 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Task2
{
	
	public partial class frmShopCalculator : Form
	{
	   
		public frmShopCalculator()
		{
			InitializeComponent();
		}

		private void lbAvailableItems_MouseDown(object sender, MouseEventArgs e)
		{
			lbAvailableItems.Sorted = true;
			if (lbAvailableItems.Items.Count == 0)
				return;

			int index = lbAvailableItems.IndexFromPoint(e.X, e.Y);
			string s = lbAvailableItems.Items[index].ToString();
			DragDropEffects DragDrop = DoDragDrop(s, DragDropEffects.All);

			if (DragDrop == DragDropEffects.All)
			{
				lbAvailableItems.Items.RemoveAt(lbAvailableItems.IndexFromPoint(e.X, e.Y));
			} 
		}

		private void lbMyCart_DragOver(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.All;
		}

		private void lbMyCart_DragDrop(object sender, DragEventArgs e)
		{
			lbMyCart.Sorted = true;
			if (e.Data.GetDataPresent(DataFormats.StringFormat))
			{
				string str = (string)e.Data.GetData(DataFormats.StringFormat);

				lbMyCart.Items.Add(str);
			}

		}

		private void btnCalculate_Click(object sender, EventArgs e)
		{
			double amount = 00.00;
			double result;

			String[] items;

			ListBox.ObjectCollection contents = lbMyCart.Items;

			foreach (string str in contents)
			{
				items = str.Split(' ');

				// assumes 3 fields: item name, a hypen, price in dollars
				Double.TryParse(items[2].Replace('$', ' '), out result);

				amount += result;
			}
			lblTotal.Text = amount.ToString(); 
		}
	
	}
}



Was This Post Helpful? 0
  • +
  • -

#9 Dellfar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 10

Re: C# listbox calculation using substring

Posted 28 January 2010 - 04:41 AM

hi,
anyone help on this code please because i have a problem in button code to add the item prices together and show the total in label?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Task2
{
	
	public partial class frmShopCalculator : Form
	{
	   
		public frmShopCalculator()
		{
			InitializeComponent();
		}

		private void lbAvailableItems_MouseDown(object sender, MouseEventArgs e)
		{
			lbAvailableItems.Sorted = true;
			if (lbAvailableItems.Items.Count == 0)
				return;

			int index = lbAvailableItems.IndexFromPoint(e.X, e.Y);
			string s = lbAvailableItems.Items[index].ToString();
			DragDropEffects DragDrop = DoDragDrop(s, DragDropEffects.All);

			if (DragDrop == DragDropEffects.All)
			{
				lbAvailableItems.Items.RemoveAt(lbAvailableItems.IndexFromPoint(e.X, e.Y));
			} 
		}

		private void lbMyCart_DragOver(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.All;
		}

		private void lbMyCart_DragDrop(object sender, DragEventArgs e)
		{
			lbMyCart.Sorted = true;
			if (e.Data.GetDataPresent(DataFormats.StringFormat))
			{
				string str = (string)e.Data.GetData(DataFormats.StringFormat);

				lbMyCart.Items.Add(str);
			}

		}

		private void btnCalculate_Click(object sender, EventArgs e)
		{
			double amount = 0.0;
			double result;

			String [] items;

			ListBox.ObjectCollection contents = lbMyCart.Items;

			foreach (string str in contents)
			{
				items = str.Split(' ');
				Double.TryParse(items[4].Replace('$', ' '), out result);

				amount += result;

			}

			lblTotal.Text = amount.ToString();

		}


	 


	
	}
}


This post has been edited by Dellfar: 28 January 2010 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1