Sunday, June 2, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  50] [ 5]  / answers: 1 / hits: 36063  / 13 Years ago, thu, february 2, 2012, 12:00:00

I am using WPF's WebBrowser control to load a simple web page. On this page I have an anchor or a button. I want to capture the click event of that button in my application's code behind (i.e. in C#).



Is there are a way for the WebBrowser control to capture click events on the loaded page's elements?



In addition, is it possible to communicate event triggered data between the page and the WebBrowser? All of the above should be possible am I right?




  • Edit: Probable solution:



I have found the following link that might be a solution. I haven't tested it yet but it's worth the shot. Will update this question depending on my test results.



http://support.microsoft.com/kb/312777




More From » c#

 Answers
9

Ok Answer found - tested and it works:




  • Add a reference from the COM tab called: Microsoft HTML Object Library



The following is an example code:



You will need two components: WebBrowser (webBrowser1) and a TextBox (textBox1)



public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

webBrowser1.LoadCompleted += new LoadCompletedEventHandler(webBrowser1_LoadCompleted);
}

private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
mshtml.HTMLDocument doc;
doc = (mshtml.HTMLDocument)webBrowser1.Document;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
}

private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
textBox1.Text = Item Clicked;
return true;
}
}

[#87677] Tuesday, January 31, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
desiraeleandrah

Total Points: 202
Total Questions: 111
Total Answers: 115

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;