C# 取消FormClosing事件时,如何将焦点设置为窗体上的控件?

C# 取消FormClosing事件时,如何将焦点设置为窗体上的控件?,c#,.net,winforms,event-handling,formclosing,C#,.net,Winforms,Event Handling,Formclosing,我正在验证表单关闭事件文本框上的输入,如果用户决定取消关闭,我将设置e.cancel=true,以停止表单关闭事件。然后我尝试将焦点设置为第一个文本框,但这不起作用 private void AddDriver_Window_FormClosing(object sender, FormClosingEventArgs e) { if (this._Cancel(sender, e)) { e.Cancel = true; this.textbox

我正在验证表单关闭事件文本框上的输入,如果用户决定取消关闭,我将设置
e.cancel=true
,以停止表单关闭事件。然后我尝试将焦点设置为第一个文本框,但这不起作用

private void AddDriver_Window_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this._Cancel(sender, e))
    {
        e.Cancel = true;
        this.textboxFirstName.Focus();
    }
}

如果您有兴趣查看
\u Cancel
方法,我问了一个不相关的问题。

我尝试了这一个检查这是否对您有效

private void AddDriver_Window_FormClosing(object sender, FormClosingEventArgs e)
{
    if( DialogResult.Cancel == MessageBox.Show("Do you want to exit programm","Alert",MessageBoxButtons.OKCancel))
    {
        e.Cancel = true;
        textBox1.Focus();
    }
}

这将弹出一个消息框,如果单击“取消”,则会自动关注文本框1。

除了文本框被禁用和这个愚蠢的_cancel()方法总是返回true等明显的内容外,没有任何可能失败的原因。阅读[