问题描述
"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"
这是我发现的Form1代码:
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();
}
}
}
}
当我尝试运行它时,VS检测到11个问题。所有错误均在Form1.Designer.cs
中Form1.Designer.cs:
错误列表:
我试图在Designer.cs的InitializeComponent()中删除事件处理程序生成的代码,更改了项目的启动对象,因为未设置该对象,并在Form1.cs中添加了Form1_Load方法,但没有任何效果。此代码有什么问题?
解决方法
当我编写WinForms应用程序时,我使用的是设计器。我很少(几乎从来没有)做类似的事情:
Form prompt = new Form() //Mentioning the Style
{
Width = 500,Height = 150,FormBorderStyle = FormBorderStyle.FixedDialog,Text = "Enter " + caption,StartPosition = FormStartPosition.CenterParent
};
prompt.Controls.Add(**whatever**);
我将向您展示如何快速获取一种形式的另一种形式。这几乎将完全在设计人员内部完成。然后,您可以将功能移到每个表单中。
摘要
- 从头开始,创建一个新的Windows Forms项目
- 在项目中添加第二个表单,这将成为您的模式对话框
- 在模式对话框中添加“确定”和“取消”按钮(您可能不需要它们,但它们很方便)
- 在主窗体上拖放一个按钮,并在按下时将其打开为形式对话框的第二个窗体。
所以,...
-
从头开始在新文件夹中创建一个新的Windows Forms / C#/。NET Framework应用程序(我将其命名为“ TestWinFormsShowDialog” )
-
右键单击TestWinFormsShowDialog项目,然后选择 Add-> Form(Windows窗体)。将表单命名为“对话框”
-
打开工具箱,并在新的 Dialog 窗体上放置两个按钮。将它们彼此相邻放置在窗体的右下角。更改以下属性
- 在第一个按钮上:
- 文本:确定
- 名称:OkBtn
- 锚点:底部,右边(您可以使用该下拉菜单)
- 在第二个按钮上:
- 文本:取消
- 名称:CancelBtn
- 锚点:右下
- 在第一个按钮上:
-
打开 Dialog 表单的属性(仍在设计器中)更改:
- AcceptButton:OkBtn
- 取消按钮:CancelBtn
-
仍然在设计器中,选择两个按钮,然后按
<Enter>
。这将为两个按钮创建按钮事件处理程序。将处理程序设置为:
代码:
private void OkBtn_Click(object sender,EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void CancelBtn_Click(object sender,EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
“模态”对话框的外壳现已完成。
- 返回主窗体的设计视图。
- 在其上放置一个按钮。根据需要放置它。设置以下属性:
- 文本:开放模式
- 名称:OpenModalBtn
- 双击按钮以创建Click处理程序,并使它看起来像:
代码:
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);
}
}
到那时,您将有一个工作模式窗口。现在,让它按照您的老师的要求去做。