1

I am loading a page in my webbrowser control & I need to capture an event in my control when an anchor element is clicked.

What i am trying to do is execute a piece of code, before the link opens the webpage dialog box.

this is the element on the webpage

<a id="saveExpression" href="#end-result" class="BuilderToolBar Save" title="Save Expression"><span class="displace"></span></a>

I have tried couple of things so far.

IServiceProvider serviceProvider = (IServiceProvider)_webBrowserCtrl.Document;
                    Guid serviceGuid = SID_SWebBrowserApp;
                    Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
                    SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);


                    SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
                    wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);

The above seems to fire only when there is a 'NewWindow', but the link opens some webpage dialog box.

I have also tried

                        mshtml.HTMLDocument doc;
                        doc = (mshtml.HTMLDocument)myWebBrowser2.Document;
                        mshtml.HTMLDocumentEvents2_Event iEvent;
                        iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
                        iEvent.onclick += new HTMLDocumentEvents2_onclickEventHandler(iEvent_onclick);

    bool iEvent_onclick(IHTMLEventObj pEvtObj)
    {
        // Code before the webpage dialog box is opened.
        return true;
    }

This captures every 'onclick' event, but then loses direction of what the click event actually does.

All i want to do is capture a click on one particular button, perform an action, before the button opens the webpage dialog. I could achieve this from the onclick but it doesnt continue to open the dialog box, the page also has a 'textarea' which loses focus when the onclick event is triggered.

<textarea name="ctl00$contentForExpressions$txtExpression" id="ctl00_contentForExpressions_txtExpression" onkeyup="commonUi.getPosition(event);expressionCurrentState.updateStatusFlag();" onmouseup="commonUi.getPosition(event)" onpaste="return commonUi.maxLengthPaste(this,&quot;160&quot;);" class="expressionBuilderTextbox" onchange="expressionCurrentState.updateStatusFlag()" onfocus="commonUi.disableToolItems()" type="text" onkeypress="commonUi.disableToolItems(this);" onmousedown="commonUi.getPosition(event)"></textarea>   

Is there a neater or rather a simpler way of achieving this? or am i missing something?

Telson Alva
  • 842
  • 6
  • 22
  • 37
  • I followed this http://stackoverflow.com/questions/6828146/how-to-capture-button-click-event-of-webpage-opened-inside-webbrowser-control Which capture events for buttons. – Telson Alva Jun 04 '15 at 13:22

1 Answers1

3

As long as you return true in iEvent_onclick, it should process the event further. Anyway, my working solution is:

private bool iEvent_onclick(IHTMLEventObj pEvtObj)
{
    if (pEvtObj.srcElement != null)
    {
        var parent = pEvtObj.srcElement.parentElement;
        if (parent != null && parent.id == "saveExpression")
        {
            MessageBox.Show("Bingo!");
            return false;
        }
    }
    return true;
}
Yegor
  • 2,514
  • 2
  • 19
  • 27
  • Thanks Yegor ... i have a textarea, when i click on it, it doesnt seem to focus after the onclick event, nor can i type anything on it. – Telson Alva Jun 04 '15 at 07:49
  • I'm not really a web guy, so it's difficult for me to say what's going on without the actual code. – Yegor Jun 04 '15 at 13:31