I have tried for days, and I cannot find a solution that explains/fits into the mess I have committed myself to.
VS 2017 C#.NET.
I have a textBox (entryBox) which feeds a prefix and a decimal into a listBox (list1), one pair at each button.Click. A foreach()-statement separates prefix and decimal, and saves decimal to a decimal-list (plaAmounts).
I wish to add up all decimals on that list, but cannot find a working way to do so. All my last textBox displays, is the most recently added decimal. Any suggestions?
class
{
private void buttonClick
{
list1.Items.Add(entryBox.Text);
for (int i = 0; i < list1.Items.Count; i++)
{
. string itemText = list1.Items.ToString();
. var plaAmounts = new List<decimal>();
.
. if (itemText.Contains("pla"))
. {
. . list1.SetSelected(i, true);
. . foreach (Object selecteditem in list1.SelectedItems)
. . {
. . . string[] substrings = itemText.Split(',');
. . . string plaNumberText = substrings[1];
. . . decimal plaNumber = Convert.ToDecimal(plaNumberText);
Question
reap123
Hi,
I have tried for days, and I cannot find a solution that explains/fits into the mess I have committed myself to.
VS 2017 C#.NET.
I have a textBox (entryBox) which feeds a prefix and a decimal into a listBox (list1), one pair at each button.Click. A foreach()-statement separates prefix and decimal, and saves decimal to a decimal-list (plaAmounts).
I wish to add up all decimals on that list, but cannot find a working way to do so. All my last textBox displays, is the most recently added decimal. Any suggestions?
class
{
private void buttonClick
{
list1.Items.Add(entryBox.Text);
for (int i = 0; i < list1.Items.Count; i++)
{
. string itemText = list1.Items.ToString();
. var plaAmounts = new List<decimal>();
.
. if (itemText.Contains("pla"))
. {
. . list1.SetSelected(i, true);
. . foreach (Object selecteditem in list1.SelectedItems)
. . {
. . . string[] substrings = itemText.Split(',');
. . . string plaNumberText = substrings[1];
. . . decimal plaNumber = Convert.ToDecimal(plaNumberText);
. . . plaAmounts.Add(plaNumber);
. . }
. }
. decimal plaTotals = plaAmounts.Sum();
. plaBox.Clear();
. plaBox.AppendText(plaTotals.ToString());
}
}
}
Link to comment
https://www.neowin.net/forum/topic/1328832-need-help-summing-up-list-of-decimals/Share on other sites
2 answers to this question
Recommended Posts