我在C#中遇到了打开和关闭窗体的新问题。关闭后处理表格
我的问题是如何处理关闭后的窗体。
这里是我的代码:
的Program.cs:
static class Program
{
public static Timer timer;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
timer = new Timer { Interval = 1000};
timer.Start();
Application.Run(new Form1());
}
}
Form1.cs中:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.ShowDialog();
/// I've tried Dispose() method . but didn't work
}
}
Form2.cs:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Program.timer.Tick += timer_Tick;
Close();
// I've tried Dispose() method instead of Close() but didn't work
}
private int count = 0;
void timer_Tick(object sender, EventArgs e)
{
count++;
if (count == 5) MessageBox.Show("");
}
}
编辑: 我的问题是:为什么消息框在5秒后显示form2已关闭!
为什么你想在垃圾收集器为你做这件事之前处理这些表单?(假设没有对表单的引用)? – Lazarus 2010-08-31 14:52:09
“它没有工作”是什么意思?是否引发异常?窗口不会消失吗? @拉扎罗斯:好问题。 – DHN 2010-08-31 14:53:05
[我需要在表单关闭后处理表单吗?](https://stackoverflow.com/a/39501121/3110834) – 2017-12-04 19:03:44