Tutorial 5-2: Balance plication in Tutorial Enhancing the Ending App that you cr
ID: 3830907 • Letter: T
Question
Tutorial 5-2: Balance plication in Tutorial Enhancing the Ending App that you created 5-8. In this tutorial, you enhance the Ending Balance form, as shown in appear at app 5-1. First, add a ListBox ntrol app each handler s the form will how run torial 5-2: modify the calculate Button click example of for the month lance in the List Figure 5.9 shows an and 8 ancing entered 1000 for the starting Ending time, when the user has Figure 5-8 The modified Ending Balance form Ication Form1.cs IDesign) Numberd Months detail ListBox Ending Balance: Step 1: Start Visual Studio and open the Ending Balance project that you completed in Tutorial 5-1. Step 2: Enlarge the form so it is roughly the size shown in Figure 5-8. (310 pixels wide by 325 pixels high should be sufficient.) Step 3: Create a Li control named detail ListBox. Resize the ListBox as shown in Figure 5-8Explanation / Answer
The enchancement in this program is to add one ListBox control into your form and create a calculatebutton_click event for display the monthly ending balance.
I have written the code for creating the ListBox control and calculatebutton_click event along with the comments for each section:-
Program for ListBox Control:
The Listbox item helps to store many list items in it.
// It is the header part of C#, which is used to embed the properties of classes and methods
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// It is the starting point of the program
namespace ListBox
{
// create a listboxform class by extending the form by using the inheritance concept.
public partial class ListBoxForm : Form
{
// Create a List Object for displaying the listItems
List<string> listItems = new List<string>();
// Create a method ListBoxForm
public ListBoxForm()
{
// It is used to add the items in the listItems object
listItems.Add("detailedListBox1");
listItems.Add("detailedListBox2");
listItems.Add("detailedListBox3");
listBox.DataSource = listItems;
}
}
}
Program for calculateButton_Click Event:
// Create a method for calculating the ending balance based on the user input
private void calculateButton_Click(object sender, EventArgs e)
{
// define the default interest rate a 0.005m
const decimal INTEREST_RATE = 0.005m;
// define the balance, months, and count variable
decimal balance;
int months;
int count = 1;
// check the if-else condition for checking the ending balance to be valid or not
if (decimal.TryParse(startingBalTextBox.Text, out balance))
{
if (int.TryParse(monthsTextBox.Text, out months))
{
// It counts the No. of months which needs to be calculated
while (count <= months)
{
// displays the final balance
balance = balance + (INTEREST_RATE * balance);
// It display the No. of items present in it.
detailListBox.Items.Add("The Final ending balance is " + "for month " + count + " is " + balance.ToString("c"));
count = count + 1;
}
// Finally, Calculate the endingBalance and displays the output on screen
endingBalanceLabel.Text = balance.ToString("c");
}
else
{
MessageBox.Show("Sorry. You have entered a Invalid value for the given months field.");
}
}
else
{
// Check whether the user entered an valid staring balance or not
MessageBox.Show("Sorry. You have entered a Invalid value for the given starting balance field.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.