本文介绍了为什么HTMLDocumentEvent的onclick激发了多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Visual C#防爆preSS 2010(.NET框架4.0版本),Windows 7 32位。

I am using Visual C# Express 2010 (.NET Framework 4.0), Windows 7 32bit.

我创建WinForm的项目,只有WebBrowser控件和ListItem对象显示由onclick事件的HTML信息。

I created WinForm project which only has WebBrowser control and ListItem object that displays the html information by onclick event.

我的目标是跑项目,然后单击文本框的网站在WebBrowser控件和显示已单击(如文本字段标签等)

My goal is to run the project and click text field in web site in the webbrowser control and display the html document info that has been clicked (such as text field tag etc)

code

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private mshtml.HTMLDocument doc = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        object oUrl = "http://livemocha.com/";
        object oEmpty = "";
        myBrowser.Navigate(oUrl.ToString());

        }


        private void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                doc = (mshtml.HTMLDocument)myBrowser.Document.DomDocument;
                mshtml.HTMLDocumentEvents2_Event iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;

                iEvent.onclick += new HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);

            }
            catch (UnauthorizedAccessException err)
            {
                Console.WriteLine("OOPS: " + err);
            }

        }

        //event handler when user clicks mouse
        //and just display info such as html tag and its attributes
        private bool ClickEventHandler(mshtml.IHTMLEventObj e)
        {
            actionList.Items.Add("------- ClickEventHandler -----------");

            actionList.Items.Add(e.type + " : " + e.srcElement.tagName + " : " + e.srcElement.outerHTML + " : " + e.srcElement.innerText + " : " + e.srcElement.id);

            return true;
        }
    }//end class
}//end namespace

现在的问题是,同时它的工作原理,ListItem对象显示相同的输出ClickEventHandler产生。举例来说,如果我去www.google.com,只是鼠标点击进入搜索文本字段,输出的是:

The problem is while it works, the ListItem object displays same output generated by ClickEventHandler. For instance, if I go to www.google.com and just mouse click into the search text field, the output is:

----- ClickEventHandler ---------
click: INPUT <INPUT style=.....
----- ClickEventHandler ---------
click: INPUT <INPUT style=.....
----- ClickEventHandler ---------
click: INPUT <INPUT style=.....

等。点击事件被触发多次。

and so on. The click events are fired multiple times.

我已经按照这里的例子: http://support.microsoft.com/kb/312777

I've followed the example here:http://support.microsoft.com/kb/312777

我在想myBrowser_DocumentCompleted()被调用多次莫名其妙地在线路中加入相同的事件处理程序,

I was thinking myBrowser_DocumentCompleted() is called multiple times somehow and adding same event handlers in the line,

 iEvent.onclick += new HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);

但HTMLDocumentEvents2_Event喜欢的onclick()方法只接受 + = - 。= 所以我真的不能改变这一行

But HTMLDocumentEvents2_Event method like onclick() only accept with += or -= so I can't really change this line.

有没有人遇到过吗?

更新

想通WebBrowser控件的URL最初设置为 www.msn.com ,该项目运行时, documentCompleted事件可以调用一次添加了单击事件处理程序。

Figured that the webbrowser control URL is initially set to www.msn.com and when the project is run, documentCompleted event is called once which adds click event handler.

然后FormLoad()事件,导航,增加了点击事件处理程序的另一个URL。

所以,现在2单击事件处理程序存在。

So now 2 click event handlers exists.

另外,我每次去不同的网址,不知何故单击事件处理程序被添加。

Plus, every time I go to different URL, somehow click event handlers are being added.

所以我pretty的肯定里面 documentCompleted()事件 iEvent没有正确清除。(通过查看code,它是每个documentCompleted()事件初始化,所以它不应该有这种行为,虽然...)

So I'm pretty sure inside of documentCompleted() event, iEvent is not properly cleared. (by looking at code, it is initialized per documentCompleted() event so it shouldn't have this behavior though...)

推荐答案

用了一段时间之后,我想,不管是页面重新加载到URL,documentCompleted()事件被触发,并同clickEvent(每次)添加到iEvent。

After spent some time, I figured that each time the page is reloaded regardless to which URL, documentCompleted() event is triggered and same clickEvent() is added to iEvent.

解决方法是检查文档为null。

The workaround is to check if doc is null.

private void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                if (doc == null)
                {
                    doc = (mshtml.HTMLDocument)myBrowser.Document.DomDocument;
                    mshtml.HTMLDocumentEvents2_Event iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;

                    iEvent.onclick += new HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
                    iEvent.onkeypress += new HTMLDocumentEvents2_onkeypressEventHandler(KeyStrokeEventHandler);
                }
            }
            catch (UnauthorizedAccessException err)
            {
                Console.WriteLine("OOPS: " + err);
            }
        }

我不相信这是最好的解决方案,但现在这个工程。 (虽然还是不知道为什么它被添加 clickEvent 尽管 iEvent 是每个 documentCompleted()电话。

这篇关于为什么HTMLDocumentEvent的onclick激发了多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:04