gpt4 book ai didi

c# - VSTO 加载项窗体关闭处理程序

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:07 24 4
gpt4 key购买 nike

我有一个用于 outlook 2013 的 VSTO 插件。我正在尝试使用事件处理程序为表单关闭事件注册一个事件。

这是我在 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 事件.

最佳答案

不需要声明 Closing 事件,因为父类提供了开箱即用的事件。此外,您可以简单地设置事件处理程序而无需声明委托(delegate)类(最新的 .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
}

关于c# - VSTO 加载项窗体关闭处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com