Clickevent两次调用函数?为什么? [英] Clickevent Calls the Function Twice?Why?

查看:101
本文介绍了Clickevent两次调用函数?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用事件处理程序调用click事件。但问题是Body_mouseDown在第一次点击时执行了2次,在第二次点击时执行了3次等等......它继续进行。我无法理解为什么?





I am calling the click event using event handler. But the problem is Body_mouseDown executes 2 times on fiest click,3 times on second click and so on...it goes on incresing. I am not able to get it WHY??


private void wbrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            kbd.Hide();
            this.wbrowser1.Document.MouseDown += new HtmlElementEventHandler(Body_MouseDown);
            currenturl = wbrowser1.Url.ToString();
            //Console.WriteLine(currenturl);
            if (currenturl != "" && currenturl != "about:blank")
            {
                //blog.fnSavePage(wbrowser1.DocumentText);
            }
                         
               
        }
         void Body_MouseDown(Object sender, HtmlElementEventArgs e)
            {
                
                HtmlElement element = this.wbrowser1.Document.GetElementFromPoint(e.ClientMousePosition);
             
               switch(e.MouseButtonsPressed)
                {

                case (MouseButtons.Left ):
                    
                    if (element != null && ("text".Equals(element.GetAttribute("type"), StringComparison.OrdinalIgnoreCase)|| "password".Equals(element.GetAttribute("type"), StringComparison.OrdinalIgnoreCase)))
                    {
                        kbd.Show();
                        showandhidekbd = false;
                    }
                    else if (element != null && element.Parent.GetElementsByTagName("div").Count >0)
                       
                    {
                        HtmlElementCollection   elements = element.Parent.GetElementsByTagName("div");
                        for (var i = 0; i < elements.Count ; i++) 
                        {
                            if (elements[i].GetAttribute("className").Contains("uic first"))
                            {
                                kbd.Hide();
                                showandhidekbd = true;
                                Console.Write(elements[i].InnerText + i  + Environment.NewLine  );
                            }
                         }
                        
                        break;
                    }
                    else
                    {
                        kbd.Hide();
                        showandhidekbd = true;
                    }
                        break;
                 case ( MouseButtons.Right):
                    
                    if (element != null && ("text".Equals(element.GetAttribute("type"), StringComparison.OrdinalIgnoreCase) || "password".Equals(element.GetAttribute("type"), StringComparison.OrdinalIgnoreCase)))
                    {
                        kbd.Show();
                        showandhidekbd = false;
                    }
                    else
                    {
                        kbd.Hide();
                        showandhidekbd = true;
                    }
                    
                break;
                }
             
               
        
            }

推荐答案

this.wbrowser1.Document.MouseDown += new HtmlElementEventHandler(Body_MouseDown);

Body_MouseDown 添加到事件处理程序队列中,即使它已经在其中。这不是调用它,而是告诉它在事件触发时调用什么方法。

因为你在每个DocumentCompleted中添加方法,所以随后会调用该方法,就像之前的文档一样。 。



既然你说这个方法在第一次点击时已经执行了2次,我怀疑你可以完全删除上面提到的行。它似乎已经在你的代码中的其他地方了。也许在某些初始化例程中,它是合适的。

adds Body_MouseDown to the event handler queue, even if it's already in it. That's not "calling" it, but telling it what method to call when the event fires.
Since you're adding the method with every DocumentCompleted, the method is then called as often as there were previous documents.

Since you're telling that the method is already executing 2 times on the first click, I suspect that you can remove above mentioned line completely. It seems to be somewhere else in your code already. Maybe in some initializing routine, where it is appropriate.


这篇关于Clickevent两次调用函数?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆