Handling clicks on buttons in the HTML page - hope this helps:

Given:

<HTML>
<BODY id=B1>
<H1>Test</H1>

<input type="BUTTON" value="Click Me!" id=button1 />
 
<P id=Test1>Test 1  x</P>
<P id=Test2>Test 2  x</P>
<P id=Test3>Test <B>3</B>  x</P>


</BODY>       
</HTML>


In Form_Load:

//Handle document complete so that we know when the web page is
available
axWebBrowser1.DocumentComplete += new
DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_Docum
entComplete);


Now in DocumentComplete:

//When the document is loaded we can work with it
private void axWebBrowser1_DocumentComplete(object sender,
DWebBrowserEvents2_DocumentCompleteEvent e) {

            //Get the HTML Document coclass
            object document = axWebBrowser1.Document;
            if (null == document) return ;

            //Get the IHTMLDocument interface
            IHTMLDocument2 doc2 = (IHTMLDocument2)document;
            
            //Now create an RCW of the correct type for the co-class -
by default it comes back as 
            //an __ComObject
            //NOTE: This will not be necessary in RTM
            HTMLDocument htmlDoc =
(HTMLDocument)Marshal.CreateWrapperOfType(document,typeof(HTMLDocument))
;

            //Now we can get the event interface and handle clicks on
the document
            //This will handle all click events because everything
bubbles up to
            //the document
            HTMLDocumentEvents2_Event htmlEvents =
(HTMLDocumentEvents2_Event)htmlDoc;
            htmlEvents.onclick += new
HTMLDocumentEvents2_onclickEventHandler(this.Doc_OnClick);


            //Get the button in the page 
            IHTMLInputButtonElement buttonElem =
(IHTMLInputButtonElement)(doc2.all.item("button1",null));

            //Get the coclass - have to make sure we get the exact
coclass here or we get an exception
            //In our case the coclass is HTMLInputButtonElement 
            HTMLInputButtonElement htmlElem =
(HTMLInputButtonElement)Marshal.CreateWrapperOfType(buttonElem,typeof(HT
MLInputButtonElement));
            
            //Test that we've got it 
            MessageBox.Show("VALUE: " + htmlElem.value);

            //Now we can get the event interface and handle events on it
            HTMLButtonElementEvents_Event buttonEvents =
(HTMLButtonElementEvents_Event)htmlElem;
            buttonEvents.onclick += new
HTMLButtonElementEvents_onclickEventHandler(this.Button1_OnClick);

        }
        
        private bool Doc_OnClick(MSHTML.IHTMLEventObj evObj) {
            string msg = "You Clicked on the HTML Page, we handled it in
managed code.\nYou clicked on ";
            MessageBox.Show(msg + evObj.srcElement.tagName  + " " +
evObj.srcElement.id);
            return true;
        }

        private bool Button1_OnClick() {
            MessageBox.Show("You Clicked on the BUTTON, we handled it in
managed code");
            return true;
        }


-----Original Message-----
From: Jade Martin [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, June 04, 2002 9:51 AM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Old Unanswer Question: How do you trap hosted IE
events?

I found a really nice tutorial at
http://www.syncfusion.com/FAQ/WinForms/FAQ_c100c.asp#q645q

I now can trap link clicks by using the BeforeNavigate event. Now I just
need to figure out how to trap button clicks.

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to