我在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