这可能吗?我需要在这里调用 Form1_FormClosing
:
ContextMenu trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Close", delegate {
Form1_FormClosing(????)
});
我需要它,因为我正在使用 _FormClosing 事件中的 CancelEventArgs:
private void Form1_FormClosing(object sender, CancelEventArgs e)
{
if (th != null && th.ThreadState == System.Threading.ThreadState.Running)
{
if (MessageBox.Show("the process is running, you want stop it?", "app", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
AbortProccess(); }
else
{
e.Cancel = true;
}
}
}
我希望这是清楚的,提前致谢。
最佳答案
您可以做一些事情。
首先,您可以使用 myForm.Close()
关闭表单,因为这将间接调用 FormClosing
事件。此外,您可以将 FormClosing
事件中的所有内容移动到单独的方法中,而不是在事件本身中。然后您可以从事件和 MenuItem 中调用该方法。如果你不想做其中任何一个,你可以尝试使用它作为委托(delegate):
//CancelEventArgs can also take a boolean which dictates if
//it should be cancelled
Form1_FormClosing(this, new CancelEventArgs());
关于c# - 如何使用控件(例如按钮)从表单调用事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8627385/