I copied a code from online. It's for my homework. The homework instruction was this:
"Write a GUI application named HomeSales that prompts the user for a salesperson initial (D, E, or F). Either uppercase or lowercase initials are valid. While the user does not type Z, continue by prompting for the amount of a sale. Issue an error message for any invalid initials entered. Keep a running tool of the amounts sold by each salesperson. After the user types Z or z for an initial, display each salesperson’s total, a grand total for all sales, and the name of the salesperson with the highest total. Format the output to up to two decimal places"
This is the Form1 code I found:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class Prompt
{
public static string ShowDialog(string text,string caption)
{
Form prompt = new Form() //Mentioning the Style
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = "Enter " + caption,
StartPosition = FormStartPosition.CenterParent
};
Label textLabel=new Label(){ Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "OK", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
//Adding the controls button,label and textbox
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
//returning the value given in the textbox
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}
}
Form2:
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 WindowsFormsApp1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
float dSales, eSales, fSales; //holds the sale amount of each sales person
string salesName; //input name
float amt; //amount getting as input
private void Form2_Load(object sender, EventArgs e)
{
dSales = eSales = fSales = 0;
display();
}
private void getSaleAmt() //function gets the sale amount and each person sales is added
{
amt = float.Parse(Prompt.ShowDialog("Enter Sale Amount ", "Sale Amount"));
if (salesName.ToUpper() == "D")
{
dSales += amt;
}
else if (salesName.ToUpper() == "E")
{
eSales += amt;
}
else if (salesName.ToUpper() == "F")
{
fSales += amt;
}
}
private void display()
{
do
{
salesName = Prompt.ShowDialog("Enter Sales Person Name", "Sales Person");
if (salesName.ToUpper() == "Z")
{
displayResult();
}
else if (salesName.ToUpper() != "D" && salesName.ToUpper() != "E" && salesName.ToUpper() != "F")
{
MessageBox.Show("Enter Valid Sales Person Name:", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
else
{
getSaleAmt();
}
} while (salesName.ToUpper() != "Z");
}
//Displays the sales details of Sales Person with grand Total and highest Sales
public void displayResult()
{
String Result;
float total;
Result = "Danielle : " + dSales.ToString()+Environment.NewLine;
Result += "Edward : " + eSales.ToString()+ Environment.NewLine;
Result += "Drancis : " + fSales.ToString()+ Environment.NewLine;
total = dSales + eSales + fSales;
Result += "Grand Total : " + total.ToString()+ Environment.NewLine;
if(dSales>eSales)
{
if(dSales>fSales)
{
Result += " Danielle has the highest Sales of " + dSales.ToString();
}
else
Result += " Francis has the highest Sales of " + fSales.ToString();
}
else if(eSales>fSales)
{
Result += " Edward has the highest Sales of " + eSales.ToString();
}
else
{
Result += " Francis has the highest Sales of " + fSales.ToString();
}
DialogResult res= MessageBox.Show(Result, "Sales Report", MessageBoxButtons.OK);
if(res==DialogResult.OK)
{
this.Close();
}
}
}
}
When I tried to run it, VS detected 11 problems. All errors were in Form1.Designer.cs
Form1.Designer.cs:
Error List:
I tried to removed the event handler generated code in InitializeComponent() in Designer.cs, changed the startup object for the project because it was not set, and added a Form1_Load method in Form1.cs, but nothing worked. What is wrong with this code?