【发布时间】:2019-11-05 17:36:02
【问题描述】:
我的申请有两种不同的形式;
- 主申请表,
- 另一个显示警告、信息、错误等(仅在需要时)。
在form 1(应用程序的主窗体)中,在不同的行中调用了第二个form,如下所示;
FormMain:
...
if (sth_goes_wrong) {
this.formWarn = new FormWarn("explanatory message...");
this.formWarn.Dispose(); // <- this is the line in question
}
...
表单警告:
internal class FormWarn : Form {
...
public FormWarn(String msg) {
this.SetupStuff();
this.btnOk.Click += FormWarn_BtnOkClick;
...
this.ShowDialog();
}
private FormWarn_BtnOkClick(Object sender, EventArgs e) {
...
this.Close();
}
...
}
警告表单显示为“对话框”,用户在阅读消息后应该按下一个按钮 (btnOk),该按钮将程序流发送到具有Close() 方法的行。
我的问题是; Close() 方法(在FormWarn 类中)是否负责释放资源,还是在返回主窗体后我需要使用Dispose() 方法?
【问题讨论】:
-
@EdPlunkett:
FormWarn的类型为Windows.Forms。我的意思是,它的类被定义为internal class FormWarn : Form {...。所以,是的;它是一个 IDisposable 类型。 -
表单类可以在没有帮助的情况下自行处理,除非它使用 ShowDialog() 显示。然后它不会,在已处理的表单对象上检索对话框属性太冒险了。在这种情况下,你总是使用
using,从不使用 Dispose()。