Ulf*_*sen 5 .net c# .net-4.0 winforms
当我显示MessageBox设置helpFilePath为某个 url 时,该 url 会加载多次。在我看来,该网址的加载次数等于我的表单父母的数量加一。
谁能解释为什么会发生这种情况?
根据MSDN,该HelpRequested事件将在活动表单上触发:
当用户单击“帮助”按钮时,
helpFilePath将打开参数中指定的帮助文件。拥有消息框的表单(或活动表单)也会接收HelpRequested事件。helpFilePath 参数的形式可以是C:\path\sample.chm 或/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");
};
}
}
Run Code Online (Sandbox Code Playgroud)
单击“新表单”几次:
然后点击“对话框”:
现在,单击帮助按钮:
在我的计算机上,这会打开 SomeHelpSite.com 树次:
我找到了一种方法来阻止不必要的行为,并且可能还解释了为什么会发生这种情况。
要阻止在第一个 URL 之后打开不需要的 URL,您只需为 HelpRequested 事件添加一个处理程序。在这种情况下,您应该通知 WinForms 引擎您已经处理了帮助请求,不需要采取进一步的操作
public Form1()
{
InitializeComponent();
this.HelpRequested += onHelpRequested;
.....
}
protected void onHelpRequested(object sender, HelpEventArgs e)
{
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
这样就只打开了一页。
现在,可能会在 MSDN 页面上针对HelpEventArgs的Handled 属性报告发生这种情况的原因,您可以在其中找到以下语句:
如果不将此属性设置为 true,则事件将传递到 Windows 进行其他处理。
编辑进一步的测试表明,即使不将 Handled 属性设置为 true,HelpRequested 事件的事件处理程序存在这一简单事实也会阻止意外行为
| 归档时间: |
|
| 查看次数: |
1255 次 |
| 最近记录: |