In this exercise, you’ll enhance the Calculator form of Lab 4 so it saves the sc
ID: 3599554 • Letter: I
Question
In this exercise, you’ll enhance the Calculator form of Lab 4 so it saves the scores the user enters in an array and then lets the user display the sorted scores in a dialog box.
1. Copy the Lab 4 project from its directory to a new directory for lab 8.
2. Open the Project from the Chapter 8 directory
3. Change the name of the Lab4.cs form to Lab8.cs 4. Be sure to add comments in your code.
5. Declare a class variable for an array that can hold up to 20 scores. (int[] scoresArray = new int[20])
6. Create an IsPresent Boolean function to ensure the contents of the score textbox is present if the add button is clicked. Pass a textbox for this function to use so a message can be displayed if the box is empty. Set the focus back to the score textbox.
7. Create an IsInt32 Boolean function to ensure the contents of the score textbox is an integer if the add button is clicked. Pass a textbox for this function to use so a message can be displayed if the box is not a number. Set the focus back to the score textbox.
8. Create an IsWithinRange Boolean function to ensure the contents of the score textbox is a range if the add button is clicked. Pass a textbox to this function to use so a message can be displayed if the box is not within the range. Declare a min variable and assign it a value of 0. Declare a max variable and assign it a value of 100. Set the focus back to the score textbox.
9. Create a IsValidData Boolean Function to return each function above. Set the min and max values in this function when returning the IsWithingRange.
10. Modify the Click event handler for the Add button Add a try/catch block Use an If statement to see if the IsValidData Boolean function passes The line above the accumulated total, add the score that’s entered by the user to the next element in the array. To do that, you can use the score count variable to refer to the element. (scoresArray[count] = score)
11. Move the Clear Scores button as shown above. Then, modify the Click event handler for this button so it removes any scores that have been added to the array. The easiest way to do that is to create a new array and assign it to the array variable. (scoresArray = new int[20])
12. Add a Display Scores button that sorts the scores in the array (Array.Sort(scoresArray)). Displays the scores in a dialog box and move the focus to the Score text box. Be sure that only the elements that contain scores are displayed. You will need a foreach loop to accumulate the scores entered in the array and assign them to a string to display in a message box. Declare a string variable to hold the scores.
foreach (int i in scoresArray)
if (i != 0)
{
scoresString += i.ToString() + " ";
}
MessageBox.Show(scoresString, "Sorted Scores");
13. Set the Average score textbox to round to 2 decimal places.
14. Test the application to be sure it works correctly.
Heres the code I currently have:
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;
namespace Lab8
{
public partial class Lab8 : Form
{
int[] scoresArray = new int[20];
int total = 0;
int count = 0;
int i = 0;
public Lab8()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
int score = Convert.ToInt32(txtScore.Text);
scoresArray[count] = score;
// total
total = total + scoresArray[i];
count += 1; // counter
// average
int average = total / count;
// displaying the text
txtScoreTotal.Text = total.ToString();
txtScoreCount.Text = count.ToString();
txtAverage.Text = average.ToString();
i++;
txtScore.Focus();
}
else
{
MessageBox.Show("Score must be between 1 and 100.", "Entry Error");
}
}
catch (Exception ex)
{
MessageBox.Show("Score is a required field", "Entry Error");
}
}
public bool IsValidData()
{
return
// Validate the Score text box
IsPresent(txtScore, "Score") &&
IsInt32(txtScore, "Score") &&
IsWithinRange(txtScore, "Score", 00, 100);
}
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
return false;
}
else
return true;
}
public bool IsInt32(TextBox textBox, string name)
{
int value;
if (int.TryParse(textBox.Text, out value))
{
return true;
}
else
return false;
}
public bool IsWithinRange(TextBox textBox, string name,
decimal min = 0, decimal max = 100)
{
if (Convert.ToInt32(textBox.Text) > max || Convert.ToInt32(textBox.Text) < min)
{
return false;
}
else
return true;
}
// display score
private void btnDisplay_Click(object sender, EventArgs e)
{
List<int> array = new List<int>();
for (int i = 0; i < count; i++)
{
array.Add(scoresArray[i]);
}
array.Sort();
string toDisplay = string.Join(Environment.NewLine, array.Select(x => x.ToString()).ToArray());
MessageBox.Show(toDisplay, "Sorted Scores");
}
// clear list
private void btnClear_Click(object sender, EventArgs e)
{
total = 0;
count = 0;
i = 0;
txtScore.Text = "";
txtScoreTotal.Text = "";
txtScoreCount.Text = "";
txtAverage.Text = "";
for (int j = 0; j < scoresArray.Length; j++)
{
scoresArray[j] = 0;
}
}
//exit the application
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Heres the designer code:
namespace Lab8
{
partial class Lab8
{
/// <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 (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtScore = new System.Windows.Forms.TextBox();
this.txtScoreTotal = new System.Windows.Forms.TextBox();
this.txtScoreCount = new System.Windows.Forms.TextBox();
this.txtAverage = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnDisplay = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtScore
//
this.txtScore.Location = new System.Drawing.Point(121, 31);
this.txtScore.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtScore.Name = "txtScore";
this.txtScore.Size = new System.Drawing.Size(132, 22);
this.txtScore.TabIndex = 0;
//
// txtScoreTotal
//
this.txtScoreTotal.Location = new System.Drawing.Point(121, 68);
this.txtScoreTotal.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtScoreTotal.Name = "txtScoreTotal";
this.txtScoreTotal.ReadOnly = true;
this.txtScoreTotal.Size = new System.Drawing.Size(132, 22);
this.txtScoreTotal.TabIndex = 1;
//
// txtScoreCount
//
this.txtScoreCount.Location = new System.Drawing.Point(121, 100);
this.txtScoreCount.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtScoreCount.Name = "txtScoreCount";
this.txtScoreCount.ReadOnly = true;
this.txtScoreCount.Size = new System.Drawing.Size(132, 22);
this.txtScoreCount.TabIndex = 2;
//
// txtAverage
//
this.txtAverage.Location = new System.Drawing.Point(121, 142);
this.txtAverage.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtAverage.Name = "txtAverage";
this.txtAverage.ReadOnly = true;
this.txtAverage.Size = new System.Drawing.Size(132, 22);
this.txtAverage.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(43, 39);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(49, 17);
this.label1.TabIndex = 4;
this.label1.Text = "Score:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(29, 71);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 17);
this.label2.TabIndex = 5;
this.label2.Text = "Score total:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(20, 103);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 17);
this.label3.TabIndex = 6;
this.label3.Text = "Score count:";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(263, 31);
this.btnAdd.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(100, 28);
this.btnAdd.TabIndex = 7;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(224, 192);
this.btnClear.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(100, 28);
this.btnClear.TabIndex = 8;
this.btnClear.Text = "Clear score";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnDisplay
//
this.btnDisplay.Location = new System.Drawing.Point(33, 192);
this.btnDisplay.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnDisplay.Name = "btnDisplay";
this.btnDisplay.Size = new System.Drawing.Size(100, 28);
this.btnDisplay.TabIndex = 9;
this.btnDisplay.Text = "Display Scores";
this.btnDisplay.UseVisualStyleBackColor = true;
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(224, 245);
this.btnExit.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(100, 28);
this.btnExit.TabIndex = 10;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(43, 150);
this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(61, 17);
this.label4.TabIndex = 11;
this.label4.Text = "Average";
//
// Lab8
//
this.AcceptButton = this.btnAdd;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(379, 322);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDisplay);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtAverage);
this.Controls.Add(this.txtScoreCount);
this.Controls.Add(this.txtScoreTotal);
this.Controls.Add(this.txtScore);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Lab8";
this.Text = "Score Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtScore;
private System.Windows.Forms.TextBox txtScoreTotal;
private System.Windows.Forms.TextBox txtScoreCount;
private System.Windows.Forms.TextBox txtAverage;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnDisplay;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label label4;
}
}
Here's the current form:
Score Calculator Sorted Scores Score: 9 Add Score total: 472 Score count: 5 Average: 94 Display Scores Clear Scores OKExplanation / Answer
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;
namespace Lab8
{
public partial class Lab8 : Form
{
int[] scoresArray = new int[20];
int total = 0;
int count = 0;
int i = 0;
public Lab8()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
int score = Convert.ToInt32(txtScore.Text);
scoresArray[count] = score;
// total
total = total + scoresArray[i];
count += 1; // counter
// average
int average = total / count;
// displaying the text
txtScoreTotal.Text = total.ToString();
txtScoreCount.Text = count.ToString();
txtAverage.Text = average.ToString();
i++;
txtScore.Focus();
}
else
{
MessageBox.Show("Score must be between 1 and 100.", "Entry Error");
}
}
catch (Exception ex)
{
MessageBox.Show("Score is a required field", "Entry Error");
}
}
public bool IsValidData()
{
return
// Validate the Score text box
IsPresent(txtScore, "Score") &&
IsInt32(txtScore, "Score") &&
IsWithinRange(txtScore, "Score", 00, 100);
}
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
return false;
}
else
return true;
}
public bool IsInt32(TextBox textBox, string name)
{
int value;
if (int.TryParse(textBox.Text, out value))
{
return true;
}
else
return false;
}
public bool IsWithinRange(TextBox textBox, string name,
decimal min = 0, decimal max = 100)
{
if (Convert.ToInt32(textBox.Text) > max || Convert.ToInt32(textBox.Text) < min)
{
return false;
}
else
return true;
}
// display score
private void btnDisplay_Click(object sender, EventArgs e)
{
List<int> array = new List<int>();
for (int i = 0; i < count; i++)
{
array.Add(scoresArray[i]);
}
array.Sort();
string toDisplay = string.Join(Environment.NewLine, array.Select(x => x.ToString()).ToArray());
MessageBox.Show(toDisplay, "Sorted Scores");
}
// clear list
private void btnClear_Click(object sender, EventArgs e)
{
total = 0;
count = 0;
i = 0;
txtScore.Text = "";
txtScoreTotal.Text = "";
txtScoreCount.Text = "";
txtAverage.Text = "";
for (int j = 0; j < scoresArray.Length; j++)
{
scoresArray[j] = 0;
}
}
//exit the application
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Heres the designer code:
namespace Lab8
{
partial class Lab8
{
/// <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 (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtScore = new System.Windows.Forms.TextBox();
this.txtScoreTotal = new System.Windows.Forms.TextBox();
this.txtScoreCount = new System.Windows.Forms.TextBox();
this.txtAverage = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnDisplay = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtScore
//
this.txtScore.Location = new System.Drawing.Point(121, 31);
this.txtScore.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtScore.Name = "txtScore";
this.txtScore.Size = new System.Drawing.Size(132, 22);
this.txtScore.TabIndex = 0;
//
// txtScoreTotal
//
this.txtScoreTotal.Location = new System.Drawing.Point(121, 68);
this.txtScoreTotal.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtScoreTotal.Name = "txtScoreTotal";
this.txtScoreTotal.ReadOnly = true;
this.txtScoreTotal.Size = new System.Drawing.Size(132, 22);
this.txtScoreTotal.TabIndex = 1;
//
// txtScoreCount
//
this.txtScoreCount.Location = new System.Drawing.Point(121, 100);
this.txtScoreCount.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtScoreCount.Name = "txtScoreCount";
this.txtScoreCount.ReadOnly = true;
this.txtScoreCount.Size = new System.Drawing.Size(132, 22);
this.txtScoreCount.TabIndex = 2;
//
// txtAverage
//
this.txtAverage.Location = new System.Drawing.Point(121, 142);
this.txtAverage.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtAverage.Name = "txtAverage";
this.txtAverage.ReadOnly = true;
this.txtAverage.Size = new System.Drawing.Size(132, 22);
this.txtAverage.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(43, 39);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(49, 17);
this.label1.TabIndex = 4;
this.label1.Text = "Score:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(29, 71);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 17);
this.label2.TabIndex = 5;
this.label2.Text = "Score total:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(20, 103);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 17);
this.label3.TabIndex = 6;
this.label3.Text = "Score count:";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(263, 31);
this.btnAdd.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(100, 28);
this.btnAdd.TabIndex = 7;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(224, 192);
this.btnClear.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(100, 28);
this.btnClear.TabIndex = 8;
this.btnClear.Text = "Clear score";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnDisplay
//
this.btnDisplay.Location = new System.Drawing.Point(33, 192);
this.btnDisplay.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnDisplay.Name = "btnDisplay";
this.btnDisplay.Size = new System.Drawing.Size(100, 28);
this.btnDisplay.TabIndex = 9;
this.btnDisplay.Text = "Display Scores";
this.btnDisplay.UseVisualStyleBackColor = true;
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(224, 245);
this.btnExit.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(100, 28);
this.btnExit.TabIndex = 10;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(43, 150);
this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(61, 17);
this.label4.TabIndex = 11;
this.label4.Text = "Average";
//
// Lab8
//
this.AcceptButton = this.btnAdd;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(379, 322);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDisplay);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtAverage);
this.Controls.Add(this.txtScoreCount);
this.Controls.Add(this.txtScoreTotal);
this.Controls.Add(this.txtScore);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Lab8";
this.Text = "Score Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtScore;
private System.Windows.Forms.TextBox txtScoreTotal;
private System.Windows.Forms.TextBox txtScoreCount;
private System.Windows.Forms.TextBox txtAverage;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnDisplay;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label label4;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.