![](/img/trans.png)
[英]White Screen Appearing When using ShowDialog method Of WPF Window after Hiding
[英]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**);
我將向您展示如何快速讓一個表單以模態方式打開另一個表單。 這將幾乎完全在設計師內部完成。 然后您可以將您的功能轉移到每個表單中。
概括
所以, ...
在一個新文件夾中從頭開始,創建一個新的 Windows Forms/C#/.NET Framework 應用程序(我將我的應用程序命名為“TestWinFormsShowDialog” )
右鍵單擊 TestWinFormsShowDialog 項目並選擇Add->Form (Windows Form) 。 將表單命名為“對話框”
打開工具箱並在新的對話框窗體上放置兩個按鈕。 將它們並排放置在表單的右下方。 更改以下屬性
打開Dialog窗體的屬性(還在設計器中)更改:
仍然在設計器中,選擇兩個按鈕並按<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();
}
模態對話框的外殼現已完成。
代碼:
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.