OnClick() event being called several times for the same element

Add-in Express™ Support Service
That's what is more important than anything else

OnClick() event being called several times for the same element
 
SilverStr




Posts: 28
Joined: 2013-03-31
Hey guys,

I am making some pretty good progress on the Addin. Way to go on a simply framework that is easy to use.

With that said, I have a weird condition that I can't seem to figure out.

Because I am intercepting so many different elements in the SupportedEvents, I understand that OnClick() could get fired lots of times. However, what I am seeing is that when I dump the srcElement of the click it is showing up several times. In other words, clicking on an element seems to show the same srcElement several times.

Here is the process how I am seeing this:


  • User clicks on an element
  • I catch the OnClick() and pop up a dialog asking some info about it
  • If the user hits cancel, I want to permit them to select another element, and as such set e.Cancel = True;


At this point, my dialog pops up again BEFORE another click. Does so 7 or 8 times. Which shows the same srcElement, which is definitely not what I am expecting.

I thought maybe somehow its nesting. So if the user clicks on an anchor that is in a div... well I would be willing to accept that the A is in one entry, then the div etc. But srcElement seems to show the tagName as the A in all entries.

Am I making sense? Is there any way to mark an OnClick() event as finished when I set Cancel so any further OnClicks() from that event are supressed so the dialog isn't poppe dup again, and again, and again. (One for every call into OnClick())
Posted 06 Apr, 2013 20:51:10 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Dana,

I will try to find a solution for you and will post a sample code.
Posted 08 Apr, 2013 09:51:20 Top
SilverStr




Posts: 28
Joined: 2013-03-31
That would be great Sergey. Its got me pretty stuck.

Looking forward to the code snippet!
Posted 08 Apr, 2013 16:24:40 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Dana,

Please try the code below:

        private bool eventsBlocked = false;

        private void adxiehtmlDocEvents1_OnClick(object sender, object eventObject, ADXCancelEventArgs e)
        {
            if (eventsBlocked) return;

            mshtml.IHTMLEventObj2 eventObj = eventObject as mshtml.IHTMLEventObj2;
            if (eventObj != null && eventObj.srcElement != null)
            {
                if (!String.IsNullOrEmpty(eventObj.srcElement.tagName))
                {
                    if (eventObj.srcElement.tagName.ToLower().Equals("a"))
                    {
                        e.ReturnDefaultValue = false;
                        e.Cancel = true;

                        this.eventsBlocked = true;

                        MessageBox.Show(this, "Anchor clicked.");
                    }
                }
            }
        }

        private void adxiehtmlDocEvents1_OnMouseUp(object sender, object eventObject)
        {
            this.eventsBlocked = false;
        }
Posted 09 Apr, 2013 10:38:41 Top
SilverStr




Posts: 28
Joined: 2013-03-31
That worked perfectly Sergey. Thanks!
Posted 13 Apr, 2013 16:03:10 Top