在 MessageBox 中单击 help-button 多次加载 help-link

Clicking help-button in MessageBox loads help-link multiple times

当我显示 MessageBoxhelpFilePath 设置为某些 url 时,url 会加载多次。在我看来,url 加载的次数等于我的表单数 parent 加一。

谁能解释为什么会这样?

根据 MSDNHelpRequested 事件将在活动表单上触发:

When the user clicks Help button, the Help file specified in the helpFilePath parameter is opened. The form that owns the message box (or the active form) also receives the HelpRequested event.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

但我不明白为什么在 parent 表单上引发 HelpRequested 事件应该从 child 表单 MessageBox 加载 link。

我是不是在做不该做的事?

此代码将重现该行为:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // First button spawns new instances of the form
        var button1 = new Button { Text = "New Form" };
        Controls.Add(button1);
        button1.Click += delegate
        {
            using (var form = new Form1())
                form.ShowDialog();
        };

        // Second button shows the MessageBox with the help-button
        var button2 = new Button { Text = "Dialog", Left = button1.Right };
        Controls.Add(button2);
        button2.Click += delegate
        {
            MessageBox.Show(
                "Press Help", 
                "Caption", 
                MessageBoxButtons.OK, 
                MessageBoxIcon.None, 
                MessageBoxDefaultButton.Button1, 
                0, // Default MessageBoxOption (probably not related to the behaviour) 
                "http://SomeHelpSite.com/MyOnlineHelp.htm");
        };
    }
}

点击"New Form"几次:

然后点击"Dialog":

现在,单击 help-button:

在我的电脑上,这会打开 SomeHelpSite.com 树次:

我找到了一种方法来阻止不受欢迎的行为,并且可能解释了为什么会发生这种情况。

要在第一个之后阻止不需要的 url 打开,您只需为 HelpRequested 事件添加一个处理程序。在这种情况下,您应该通知 WinForms 引擎您已经处理了帮助请求并且不需要进一步的操作

public Form1()
{
    InitializeComponent();
    this.HelpRequested += onHelpRequested;
    .....
}
protected void onHelpRequested(object sender, HelpEventArgs e)
{
    e.Handled = true;
}

这样,只打开了一页。

现在,可能会在 HelpEventArgsHandled property 的 MSDN 页面上报告发生这种情况的解释,您可以在其中找到以下语句:

If you do not set this property to true the event will be passed to Windows for additional processing.

EDIT 进一步的测试表明,即使没有将 Handled 属性 设置为 true,HelpRequested 事件的事件处理程序存在这一简单事实也能阻止不良行为