I need to sum a looped listbox in C#. I am adding a snip of my code. The user is to input the number of days that were worked. You get paid .01 for the first day and then that amount doubles per day worked. The code I am posting already does what it is supposed to do - it displays a list with the numbers of days worked with the amount of pay for that day listed beside it. I need to sum up the daily pay totals into one amount (a total pay), which I am displaying in a label. Can you tell me what code I need/am missing?
private void btnCalculate_Click(object sender, EventArgs
e)
{ //Validate the entry is numeric.
try
{
/*Declare a variable for the number of days worked and
* convert the input value from the textbox into an integer.*/
int intDaysWorked = Convert.ToInt32(txtDaysInput.Text);
//Declare a variable to handle the looping.
int intCounter = 1;
//Declare a variable to hold the pay.
double dblDay = .01;
//Declare a variable to do the math.
double dblResult = dblDay;
//Validate the number entered is a positive whole number.
if (intDaysWorked > 0)
{
//Set the conditions for the loop
while (intCounter <= intDaysWorked)
{
//Add to the listbox and add one to the counter.
lstDailyPay.Items.Add("Pay for Day" + " " + intCounter + " " + "="
+ " " + dblResult.ToString("c"));
intCounter++;
//Do the math for the loop.
dblResult = dblResult * 2;
Application Name :DoubleAmount
Language used :C#
User Interface (Form1.cs[Design]) :
********************************
Form1.cs :
//namespace
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//application namespace
namespace DoubleAmount
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//calculate button click
private void btnCalculate_Click(object sender, EventArgs e)
{
//Validate the entry is numeric.
try
{
/*Declare a variable for the number of days worked and
* convert the input value from the textbox into an integer.*/
int intDaysWorked = Convert.ToInt32(txtDaysInput.Text);
//Declare a variable to handle the looping.
int intCounter = 1;
//Declare a variable to hold the pay.
double dblDay = .01;
//Declare a variable to do the math.
double dblResult = dblDay;
//declare a variable to store total pay
double totalPay = 0;
//Validate the number entered is a positive whole number.
if (intDaysWorked > 0)
{
//Set the conditions for the loop
while (intCounter <= intDaysWorked)
{
//Add dblResult into variable totalPay
totalPay = totalPay + dblResult;
//Add to the listbox and add one to the counter.
lstDailyPay.Items.Add("Pay for Day" + " " + intCounter + " " + "="
+ " " + dblResult.ToString("c"));
intCounter++;
//Do the math for the loop.
dblResult = dblResult * 2;
}
//display total pay in the label
lblTotalPay.Text = "Total Pay : " + totalPay.ToString();
}
}//if any exception handle it
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
======================================
Screen showing total pay and pay for each day :
Get Answers For Free
Most questions answered within 1 hours.