Question

I need to sum a looped listbox in C#. I am adding a snip of my...

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;
  

Homework Answers

Answer #1

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 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
I am trying to solve the following C++ problem over structures. I am having trouble with...
I am trying to solve the following C++ problem over structures. I am having trouble with the very last structure and I made a comment next to where I am confused. I included the assignment instructions and my current code. I would appreciate if anyone can help me explain what I need to do for the last structure. Thank you. Assignment Instructions Create a program that will prompt me for each of the following items: 1. Date of Birth 2....
Microsoft Visual Studio in C#: I have this code and need to loop through each element...
Microsoft Visual Studio in C#: I have this code and need to loop through each element of the patient's array and add to a listbox (lbAccountDisplay) each property of the patient's object. This is the 2nd time to ask this question; it should have all the detail you need to help me. It is a piece of a project and I am trying not to give you the whole project. How do I use a foreach loop to loop through...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
I am trying to make a program in C# and was wondering how it could be...
I am trying to make a program in C# and was wondering how it could be done based on the given instructions. Here is the code that i have so far... namespace Conversions { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if...
I am establishing a savings account paying 6% annual return to fund a portion of my...
I am establishing a savings account paying 6% annual return to fund a portion of my son’s college tuition. I want to make a withdrawal of an equal amount from the savings at the end of each of his first 4 years of college. I will have $24,256 in the savings account when he begins college and will keep the account open for 4 years. Identify the details below that I need in order to determine the dollar amount I...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT