c# - 单击 MessageBox 中的帮助按钮多次加载帮助链接

标签 c# .net winforms .net-4.0

当我显示 MessageBox 并将 helpFilePath 设置为某个 url 时,该 url 会加载多次。在我看来,该 url 的加载次数等于我的表单 parent 的数量加一。

谁能解释为什么会这样?

根据 MSDN HelpRequested 事件将在事件表单上触发:

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.

但我不明白为什么在父窗体上引发 HelpRequested 事件应该从子窗体 MessageBox 加载链接。

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

此代码将重现该行为:

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");
        };
    }
}

点击“新建表单”几次:

enter image description here

然后点击“对话框”:

enter image description here

现在,单击帮助按钮:

enter image description here

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

enter image description here

最佳答案

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

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

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

这样就只打开了一个页面。

现在可能会在 Handled property 的 MSDN 页面上报告为什么会发生这种情况。在 HelpEventArgs 中,您可以在其中找到此语句:

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

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

关于c# - 单击 MessageBox 中的帮助按钮多次加载帮助链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458602/

相关文章:

.net - ADO.net 关闭 TCP 连接的速度不够快

c# - 我如何 "date-protect"我的应用程序?

c# - 从 HttpWebRequest 和 HttpWebResponse 获取 Http 状态代码(200、301、404 等)

c# - 用于查找具有非法字符的属性的 XPath

c# - 我不应该捕获哪些异常?

c# - 如何在MySql中以Nvarchar类型(过程参数)传递DBNull

c# - System.ObjectDisposedException : 'Cannot access a disposed object.'

c# - 如何在 WPF 或 WinForms 中呈现公式

带有可视化编程编辑器的 C# 应用程序

c# - 文本框,不带重音符号的搜索(忽略重音符号)