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:...
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...
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...
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...
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....
BASIC c++ question Please do not try to fix this, I just have a question regarding...
BASIC c++ question Please do not try to fix this, I just have a question regarding the function of this code. I understand this is an infinite loop, however, why is it that when I enter '0', it continues to ask the user for an input, but when I enter a letter, it continues infinitiy. And when it does infinitely loop, why is it that it continues to add 1 to each number? Doesn't the loop go through each line...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT