-2

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:

enter image description here

Error List:

enter image description here

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?

1 Answers1

1

When I write a WinForms app, I use the designer. I very rarely (nearly never) do things like:

Form prompt = new Form() //Mentioning the Style
{
    Width = 500,
    Height = 150,
    FormBorderStyle = FormBorderStyle.FixedDialog,
    Text = "Enter " + caption,
    StartPosition = FormStartPosition.CenterParent
};
prompt.Controls.Add(**whatever**);

I will show you how to quickly get one form opening another modally. This will be done nearly entirely within the designer. Then you can move your functionality over into each of the forms.

Summary

  1. Start over from scratch, creating a new Windows Forms project
  2. Add a second form to the project that will become your modal dialog
  3. Add OK and Cancel buttons to the modal dialog (you may not want them, but they can be handy)
  4. Drop a button on the main form, and have it open the second form as a modal dialog when pressed.

So, ...

  • Start over from scratch in a new folder, creating a new Windows Forms/C#/.NET Framework application (I named mine "TestWinFormsShowDialog")

  • Right-click the TestWinFormsShowDialog project and choose Add->Form (Windows Form). Name the form "Dialog"

  • Open the toolbox and drop two buttons on your new Dialog form. Position them next to each other in the lower-right of the form. Change the following properties

    • On the first button:
      • Text: OK
      • Name: OkBtn
      • Anchor: Bottom, Right (you can use the nice drop down for that)
    • On the second button:
      • Text: Cancel
      • Name: CancelBtn
      • Anchor: Bottom, Right
  • Open the properties of the Dialog form (still in the designer) change:

    • AcceptButton: OkBtn
    • CancelButton: CancelBtn
  • Still in the designer, select both buttons and press <Enter>. This will create button event handlers for the two buttons. Set the handlers to look like:

Code:

private void OkBtn_Click(object sender, EventArgs e)
{
    DialogResult = DialogResult.OK;
    Close();
}

private void CancelBtn_Click(object sender, EventArgs e)
{
    DialogResult = DialogResult.Cancel;
    Close();
}

The shell of your Modal dialog is now complete.

  • Go back to the design view of your main form.
  • Drop a button on it. Position it how you want. Set these properties:
    • Text: Open Modal
    • Name: OpenModalBtn
  • Double-click the button to create a Click handler and make it look like:

Code:

private void OpenModalBtn_Click(object sender, EventArgs e)
{
    var modal = new Dialog();
    if (modal.ShowDialog(this) == DialogResult.OK)
    {
        MessageBox.Show(this, "You pressed OK", "Hey, it worked", MessageBoxButtons.OK,
            MessageBoxIcon.Information);
    }
}

At that point, you will have a working modal window. Now make it do what your instructor wants.

Flydog57
  • 6,851
  • 2
  • 17
  • 18