Goozo
Goozo

Reputation: 956

Why the external IE is openings from .NET webbrowser

I have this form which has a System.Windows.Forms.WebBrowser controller in it. One of the html pages has some href in it like this

 <a href="https://twitter.com/xxx" target="_blank">
   <h3 style="margin-top:15%; text-align:center">xxx</h3>
 </a>

I am catching the clicks in the

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

and opening the page as so

webBrowser1.Navigate(page);

Now this works and the page is shown inside the webBrowser1, but this also opens the external IE with the same page. Actually, the external IE opens first before the webBrowser1 shows the page.

So my question is: Why does the external IE opens and how can I stop this.

cheers, es

Upvotes: 0

Views: 44

Answers (1)

Alexey Obukhov
Alexey Obukhov

Reputation: 854

Instead of opening a new page try to create custom onClick event

private bool bCancel = false;

private void webBrowser_DocumentCompleted(object sender,
                                 WebBrowserDocumentCompletedEventArgs e)
{
  int i;
  for (i = 0; i < webBrowser.Document.Links.Count; i++)
  {
     webBrowser.Document.Links[i].Click += new    
                            HtmlElementEventHandler(this.LinkClick);
  }
}
private void LinkClick(object sender, System.EventArgs e)
{
  bCancel = true;
  MessageBox.Show("Link Was Clicked Navigation was Cancelled");
}
private void webBrowser_Navingating(object sender, 
                                WebBrowserNavigatingEventArgs e )
{
  if (bCancel == true)
  {
     e.Cancel=true;
     bCancel = false;
  }
}

Upvotes: 1

Related Questions