首页 > 解决方案 > ShowDialog 没有出现

问题描述

我从网上复制了一个代码。这是为了我的家庭作业。作业指导是这样的:

"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 : "";
        }
    }
}

表格2:

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 方法,但没有任何效果。这段代码有什么问题?

标签: c#winformsuser-interfaceshowdialog

解决方案


当我编写 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**);

我将向您展示如何快速让一个表单打开另一个模态。这将几乎完全在设计器内完成。然后,您可以将您的功能转移到每个表单中。

概括

  1. 从头开始,创建一个新的 Windows 窗体项目
  2. 将第二个表单添加到将成为您的模态对话框的项目中
  3. 在模态对话框中添加 OK 和 Cancel 按钮(您可能不需要它们,但它们很方便)
  4. 在主窗体上放置一个按钮,并在按下时将第二个窗体作为模式对话框打开。

所以, ...

  • 在一个新文件夹中从头开始,创建一个新的 Windows Forms/C#/.NET Framework 应用程序(我将我的命名为“TestWinFormsShowDialog”

  • 右键单击 TestWinFormsShowDialog 项目并选择Add->Form (Windows Form)。将表单命名为“对话框”

  • 打开工具箱并在新的Dialog表单上放置两个按钮。将它们并排放置在表单的右下角。更改以下属性

    • 在第一个按钮上:
      • 文字:好的
      • 名称:OkBtn
      • 锚点:底部,右侧(您可以使用漂亮的下拉菜单)
    • 在第二个按钮上:
      • 文本:取消
      • 名称:CancelBtn
      • 锚点:底部,右侧
  • 打开Dialog表单的属性(仍在设计器中)更改:

    • 接受按钮: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);
    }
}

此时,您将拥有一个工作模式窗口。现在让它做你的教练想要的。


推荐阅读