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
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array size:...
4) Write a java program where you will use while loop, where if condition is equal...
4) Write a java program where you will use while loop, where if condition is equal to 2 (or any other choice of your number) then it will break the while condition. Steps: 1) Declare variable int (any name) which is equal to zero 2) Declare while condition which variable int is less then equal to 10 3) Declare print out where the value of variable is the same name that you declared in step 1 4) Declare condition if...
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 need a few unit tests done on my SelectionSort program in Visual Studio 2019 with...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with MSTest. My program is below. I just need screen shots of the unit test code in Visual Studio 2019 with MSTest. Please and thank you. using System; namespace SelectionSortAlgorithm { public class SelectionSort { public static void Main(string[] args) { int[] dataSet = new int[5] { 2, 99, 27, 68, 3 }; // 5 element array initialized int n = 5; // variable declar...
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...
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
Hello i am working on a project for my programming course and i am a little...
Hello i am working on a project for my programming course and i am a little lost. The assignment is as follows: Part A Do all of problem 3.13 (Employee Class), including the main, as written. Part B Now, suppose a new company policy says that instead of the monthly salary we will now store the yearly salary. We will need to make this change inside Employee and support the new usage, without breaking existing code (such as the old...
Need this in C programming please with correct output of the code and also please attach...
Need this in C programming please with correct output of the code and also please attach the screenshot of it // In this problem, we try to find and fix a bug in the following code. // The following code is trying to calculate a sum that will converge to PI(3.141592....) // when the number of items goes to infinity. // This following code is based on the fact that // that sum of items 8/((4*i +1)*(4*i +3)) for i...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT