【发布时间】:2011-04-06 07:49:38
【问题描述】:
我遇到了在 C# 中打开和关闭表单的新问题。
我的问题是关闭后如何处理表单。
这是我的代码:
程序.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("");
}
}
已编辑: 我的问题是:为什么form2关闭后5秒后显示消息框!
【问题讨论】:
-
为什么要在垃圾收集器为您执行此操作之前处理表单(假设没有对剩余表单的引用)?
-
“它不起作用”是什么意思?是否引发异常?窗口不会消失吗? @Lazarus:好问题。