Open Link in New Tab (Webbrowser Control)

Open link in new TAB (WebBrowser Control)

Based on your comments, I understand that you want to trap the "Open In New Window" action for the WebBrowser control, and override the default behavior to open in a new tab inside your application instead.

To accomplish this reliably, you need to get at the NewWindow2 event, which exposes ppDisp (a settable pointer to the WebBrowser control that should open the new window).
All of the other potential hacked together solutions (such as obtaining the last link selected by the user before the OpenWindow event) are not optimal and are bound to fail in corner cases.

Luckily, there is a (relatively) simple way of accomplishing this while still using the System.Windows.Forms.WebBrowser control as a base. All you need to do is extend the WebBrowser and intercept the NewWindow2 event while providing public access to the ActiveX Instance (for passing into ppDisp in new tabs). This has been done before, and Mauricio Rojas has an excellent example with a complete working class "ExtendedWebBrowser":

http://blogs.artinsoft.net/mrojas/archive/2008/09/18/newwindow2-events-in-the-c-webbrowsercontrol.aspx

Once you have the ExtendedWebBrowser class, all you need to do is setup handlers for NewWindow2 and point ppDisp to a browser in a new tab. Here's an example that I put together:

    private void InitializeBrowserEvents(ExtendedWebBrowser SourceBrowser)
{
SourceBrowser.NewWindow2 += new EventHandler<NewWindow2EventArgs>(SourceBrowser_NewWindow2);
}

void SourceBrowser_NewWindow2(object sender, NewWindow2EventArgs e)
{

TabPage NewTabPage = new TabPage()
{
Text = "Loading..."
};

ExtendedWebBrowser NewTabBrowser = new ExtendedWebBrowser()
{
Parent = NewTabPage,
Dock = DockStyle.Fill,
Tag = NewTabPage
};

e.PPDisp = NewTabBrowser.Application;
InitializeBrowserEvents(NewTabBrowser);

Tabs.TabPages.Add(NewTabPage);
Tabs.SelectedTab = NewTabPage;

}

private void Form1_Load(object sender, EventArgs e)
{

InitializeBrowserEvents(InitialTabBrowser);

}

(Assumes TabControl named "Tabs" and initial tab containing child control docked ExtendedWebBrowser named "InitialWebBrowser")

Don't forget to unregister the events when the tabs are closed!

How to open a link in webBrowser control in external browser?

The easiest way to do this would be to intercept the Navigating event.

public void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel the current event
e.Cancel = true;

//this opens the URL in the user's default browser
Process.Start(e.Url.ToString());
}

How to open url in new browser tab from Web browser control (displaying PDF having a link) c#

Hope this help you. It worked for me.

Add the Navigating event on your webBrowser control. This will open the link in a new Browser window. In my case Google Chrome.

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
try
{
if (!(e.Url.ToString().ToLower().Contains("file") || e.Url.ToString().ToLower().Contains("pdf")))
{
e.Cancel = true;
//Open Link
Process.Start(e.Url.ToString());
}
}
catch (Exception err)
{
//Handle Exception
}
}

WebBrowser, right click on links to open new tab

I solve it.
It's simple this command line will make IE silent.

e.Cancel = True

You don't need to build other menu and turn off the main menu of the IE then build all Items again.

This is the final code to open links when you click "right click on them" and will be opened in a new tab

 Private Sub WebBrowser1_NewWindow1(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
e.Cancel = True

Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim Browser As New WebBrowser
Dim targeturl As String = thiselement.GetAttribute("href")
With Browser
.Navigate(targeturl)
.Dock = DockStyle.Fill
End With

TabControl1.TabPages.Add(targeturl)
TabControl1.SelectTab(int)
TabControl1.SelectedTab.Controls.Add(Browser)
AddHandler Browser.ProgressChanged, AddressOf Loading
AddHandler Browser.DocumentCompleted, AddressOf Done
int = int + 1

End Sub

WebBrowser-Control - open default browser on click on link

You can open the new page in default browser using Proces.Start() on Navigating event and set e.Cancel = true; so that the page in the control will not change.

Example:

@MainWindow.xaml.cs

using System.Diagnostics;
using System.Windows;
using System.Windows.Navigation;

namespace OpenDefaultBrowser
{
public partial class MainWindow : Window
{
private static bool willNavigate;

public MainWindow()
{
InitializeComponent();
}

private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
// first page needs to be loaded in webBrowser control
if (!willNavigate)
{
willNavigate = true;
return;
}

// cancel navigation to the clicked link in the webBrowser control
e.Cancel = true;

var startInfo = new ProcessStartInfo
{
FileName = e.Uri.ToString()
};

Process.Start(startInfo);
}
}
}

@MainWindow.xaml

<Window x:Class="OpenDefaultBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="464" Width="1046">
<Grid>
<WebBrowser Height="425" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="1024" Source="http://stackoverflow.com/" Navigating="webBrowser1_Navigating" />
</Grid>
</Window>


Related Topics



Leave a reply



Submit