ShowDialog 没有出现
Posted
技术标签:
【中文标题】ShowDialog 没有出现【英文标题】:ShowDialog not appearing 【发布时间】:2021-02-11 13:21:33 【问题描述】:我从网上复制了一个代码。这是为了我的家庭作业。作业指导是这样的:
"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 方法,但没有任何效果。这段代码有什么问题?
【问题讨论】:
【参考方案1】:当我编写 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 窗体项目
向项目中添加第二个表单,该表单将成为您的模态对话框
在模态对话框中添加“确定”和“取消”按钮(您可能不需要它们,但它们很方便)
在主窗体上放置一个按钮,按下时让它以模式对话框的形式打开第二个窗体。
所以,...
在一个新文件夹中从头开始,创建一个新的 Windows Forms/C#/.NET Framework 应用程序(我将我的命名为 “TestWinFormsShowDialog”)
右击TestWinFormsShowDialog 项目并选择Add->Form (Windows Form)。将表单命名为“对话框”
打开工具箱并在您的新 Dialog 表单上放置两个按钮。将它们并排放置在表单的右下角。更改以下属性
在第一个按钮上: 文字:好的 名称:OkBtn 锚点:底部,右侧(您可以使用 nice 下拉菜单) 在第二个按钮上: 文本:取消 名称:CancelBtn 锚点:右下角打开Dialog表单的属性(还在设计器中)更改:
接受按钮:OkBtn CancelButton: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);
此时,您将拥有一个工作模式窗口。现在让它按照你的导师的要求去做。
【讨论】:
以上是关于ShowDialog 没有出现的主要内容,如果未能解决你的问题,请参考以下文章
使用 form.ShowDialog() 时,主窗体中的线程不起作用
Application.Run()和Form.Show()以及Form.ShowDialog()
在构建期间调用颤振 showdialog setState() 或 markNeedsBuild()