提问人:Thibault de Villèle 提问时间:4/28/2015 最后编辑:John SaundersThibault de Villèle 更新时间:5/1/2015 访问量:56
在 MenuStripItem 上退出应用程序
Quit application on MenuStripItem press
问:
我是 C# 的新手,今天遇到了一个错误。
我正在 Visual Studio 2013 中用 Visual C# 制作一个 Windows 窗体应用程序。 在项目的“Form1 [设计]”选项卡中,我添加了一个 MenuStrip,然后在其中创建了一个“新建”和“退出”项。
当我按下“退出”按钮(此处标识为 quitterLapplicationToolStripMenuItem,由 VS2013 自动生成)时,我有以下代码要运行:
private void quitterLapplicationToolStripMenuItem_Click(object sender, CancelEventArgs c, EventArgs e)
{
DialogResult resultat = MessageBox.Show("Close?" + Environment.NewLine + Environment.NewLine + "Really ? No more notifications ?", "Closing", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (resultat == DialogResult.Yes)
{
MessageBox.Show("Prog stopped correctly", "Quit");
Application.Exit();
}
else
{
c.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
}
当我尝试运行它时,出现一个错误,说:
“quitterLapplicationToolStripMenuItem_Click”匹配项不会过载 委托“System.EventHandler”
哦,这是导致错误的行:
this.quitterLapplicationToolStripMenuItem.Click += new System.EventHandler(this.quitterLapplicationToolStripMenuItem_Click);
我能做些什么?我被困住了,我没有找到任何可以帮助我的东西(而且我能理解)
答:
3赞
Leon Bambrick
4/28/2015
#1
取代:
(object sender, CancelEventArgs c, EventArgs e)
跟
(object sender, EventArgs e)
该方法的参数不正确:您得到了两种不同方法签名的混合体。
它应该是:
private void quitterLapplicationToolStripMenuItem_Click(object sender, CancelEventArgs c)
或
private void quitterLapplicationToolStripMenuItem_Click(object sender, EventArgs e)
评论
0赞
Thibault de Villèle
4/28/2015
感谢您的回答,但是现在,由于EventArgs中没有“取消”定义,因此导致我出错。有没有另一种方法可以取消关闭程序的操作?c.Cancel
1赞
Thibault de Villèle
4/28/2015
没关系,只是删除 c.Cancel 参数就完成了我想做的事情。
评论