我有一个面向Outlook 2013的VSTO插件。我正在尝试使用事件处理程序为Form关闭事件注册一个事件。
以下是我在Form1类中的代码:
public delegate void MyEventHandler();
private event MyEventHandler Closing;
private void OtherInitialize()
{
this.Closing += new MyEventHandler(this.Form1_Closing);
}
同样来自Form1:
public Form1()
{
InitializeComponent();
OtherInitialize();
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
// Not sure what to put here to make the application exit completely
// Looking for something similar to Pytthon's sys.exit() or
// Applicaton.Exit() in Forms Applicatons, I tried
// Applicaton.Exit() it did not work
}
当我运行此操作时,我收到错误并发出警告:
警告:
Form1.Closing hides inherited member System.Windows.Forms.Form.Closing. Use the new keyword if hiding was intended
错误:
No overload for Form1_Closing matches delegate System.EventHandler
这些错误/警告意味着什么?如何使用X按钮或form.Close()关闭窗体时,如何正确注册Form1_Closing事件处理程序现在我可以调用form.Close()但它似乎没有触发Form1_Closing事件
答案 0 :(得分:2)
无需声明Closing事件,因为父类提供了开箱即用的事件。此外,您可以简单地设置事件处理程序而不声明委托类(最新的.net版本):
public Form1()
{
InitializeComponent();
OtherInitialize();
}
private void OtherInitialize()
{
Closing += Form1_Closing;
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
// Not sure what to put here to make the application exit completely
// Looking for something similar to Pytthon's sys.exit() or
// Applicaton.Exit() in Forms Applicatons, I tried
// Applicaton.Exit() it did not work
}