簡體   English   中英

ShowDialog 沒有出現

[英]ShowDialog not appearing

我從網上復制了一個代碼。 這是我的家庭作業。 作業指導是這樣的:

"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 方法,但沒有任何效果。 這段代碼有什么問題?

當我編寫 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) 將表單命名為“對話框”

  • 打開工具箱並在新的對話框窗體上放置兩個按鈕。 將它們並排放置在表單的右下方。 更改以下屬性

    • 在第一個按鈕上:
      • 文字:好的
      • 名稱: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);
    }
}

此時,您將擁有一個工作模式窗口。 現在讓它做你的導師想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM