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;
C# listbox calculation using substringadding items using substring
Page 1 of 1
7 Replies - 9349 Views - Last Post: 28 January 2010 - 04:41 AM
#1
C# listbox calculation using substring
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...^.^
#3
Re: C# listbox calculation using substring
Posted 15 January 2010 - 11:53 PM
Here is maybe an easier way:
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
{
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
#4
Re: C# listbox calculation using substring
Posted 16 January 2010 - 12:12 AM
ohh it works..thank you..=))
#5
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...
http://www.dreaminco...topic145733.htm
Hope this helps...
#6
Re: C# listbox calculation using substring
Posted 27 January 2010 - 02:36 AM
hi,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
#7
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
#8
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();
}
}
}
#9
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?
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|