我正在尝试重置我的主表单,以便我可以轻松地重置所有文本框和变量。我在我的 Progam.cs 中添加了一个布尔值,以使应用程序在表单关闭然后重新打开时保持打开状态。当我尝试关闭它时, on_closure 甚至会触发两次。我不知道该怎么做才能阻止它发生,但我知道它必须是简单的。
程序.cs:
static class Program
{
public static bool KeepRunning { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
KeepRunning = true;
while (KeepRunning)
{
KeepRunning = false;
Application.Run(new Form1());
}
}
}
表格1:
private void button1_Click(object sender, EventArgs e)
{
Program.KeepRunning = true;
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialogResult = MessageBox.Show("You have unsaved work! Save before closing?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (dialogResult == DialogResult.Yes)
{
e.Cancel = true;
MessageBox.Show("saving then closing");
Application.Exit();
}
if (dialogResult == DialogResult.No)
{
MessageBox.Show("closing");
Application.Exit();
}
if (dialogResult == DialogResult.Cancel)
{
e.Cancel = true;
MessageBox.Show("canceling");
}
}