【问题标题】:Windows forms lost focus when two consecutive MessageBox pop are fired当两个连续的 MessageBox pop 被触发时,Windows 窗体失去焦点
【发布时间】:2017-07-11 13:02:09
【问题描述】:
Class MainForm:Form {
    Public CheckValidation()
    {
    var controller = new FormController();
    controller.checkValidation();
    }
}

    class FormController {
    public checkValidation ()
{
    MessageBox.Show('test_a',MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
    MessageBox.Show('test_b',MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
    }
}

问题是第一个消息框弹框关闭后,焦点就丢给了windows上的一些其他应用,比如outlook。

期望是焦点被发送到第二个消息框弹出,排队等待下一个执行。这样我就不必手动单击第二个弹出框以使其处于活动状态以将其关闭。并且在第二个弹出框关闭后焦点返回到主窗体。

【问题讨论】:

  • 这将不容易控制。 Windows 在焦点问题上变得越来越糟。您可以尝试通过将第一个参数设置为 this 来锚定 MessageBox。

标签: c# forms winforms messagebox


【解决方案1】:

解决方案: 删除 'MessageBoxOptions.ServiceNotification' 参数有效。

替代解决方案 1:

如果我们希望明确指定消息框应始终位于最前面和最顶部的窗口中,我们可以指定参数 ((0x40000),它是 MB_TOPMOST 选项的标志。

MessageBox.Show('test_a', MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); //Message Box Top Most (MB_TOPMOST) = 0x40000

替代方案 2: 另一种明确指定消息框应始终位于最前面和最顶部的窗口的方法是,我们可以创建一个新的表单对象并将“TopMost”布尔选项设置为“True”。

MessageBox.Show(new Form() { TopMost = true }, 'test_b', MessageBoxButtons.OK, MessageBoxDefaultButton.Button1);

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多