【问题标题】:How to set focus to a Control on the Form when cancelling FormClosing event?取消 FormClosing 事件时如何将焦点设置到窗体上的控件?
【发布时间】:2014-04-27 02:44:39
【问题描述】:

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

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

如果您有兴趣查看._Cancel 方法,我问了一个不相关的问题earlier

【问题讨论】:

  • 除了文本框被禁用和这个愚蠢的 _Cancel() 方法总是返回 true 等明显的东西之外,没有任何可以想象的原因这可能会失败。阅读 [sscce.com]

标签: c# .net winforms event-handling formclosing


【解决方案1】:

我试过这个,看看这对你有用吗..

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。

【讨论】:

    猜你喜欢
    • 2011-05-02
    • 2017-06-15
    • 2016-10-22
    • 2014-11-22
    • 2011-10-26
    • 2017-01-21
    • 2014-01-21
    • 1970-01-01
    • 2010-12-09
    相关资源
    最近更新 更多