views:

7923

answers:

5

Hi there,

Does anybody know how to click on a link in the WebBrowser Control in a C# Windows Forms Application and then have that link open in a new tab inside my TabControl?

I've been searching for months, seen many tutorials/articles/code samples but it seems as though nobody has ever tried this in C# before.

Any advice/samples are greatly appreciated.

Thank you, Baeltazor

+2  A: 
private Uri _MyUrl;    

System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(browser_Navigating);


void browser_Navigating(object sender, System.Windows.Forms.WebBrowserNavigatingEventArgs e)
{
    _MyUrl = e.Url;
    e.Cancel;
}
sshow
Thank you sshow, that was very helpful, though I am trying to get the url that is sent to the new browser window so that i can stop the new browser from opening and just open that url in a new tab in my own browser? For example when i right-click on a link and select Open in New Window i don't want it to open in a new IE i want it to open in a new tab in my own webbrowser
baeltazor
Maybe you can override the ContextMenu for the "Open in New Window" button. I've never done such a thing before. Good Luck!
sshow
Thank you sshow if i'm successful i'll post code for everyone incase they'd like to know as I've never seen anybody do this anywhere on the web in c# before...
baeltazor
+5  A: 

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!

Robert Venables
Thank you very much Robert for the sample code and the link :-) I do have a couple of questions, I added a class and called it ExtendedWebBrowser.cs and then added your sample code to the Form1.cs file and it gives me the following error when I try to debug: "Argument '1' cannot convert from 'System.Windows.Forms.WebBrowser to 'ExtendedWebBrowser'." And the blue squigly line appears below the following line of code: InitializeBrowserEvents(InitialTabBrowser); Do you know what that is?
baeltazor
Make sure your InitialWebBrowser is of type "ExtendedWebBrowser" - It looks like you are still using the "WebBrowser". To add the ExtendedWebBrowser, look in your toolbox. You should see it towards the top with a small gear symbol next to it. This is what you should add to your form in design mode for the InitialWebBrowser control.
Robert Venables
thank you so much for this Robert. You've been so helpful. :)
baeltazor
A: 

There is no tabbing in the web browser control, therefor you need to handle the tabs yourself. Add a tab control above the web browser control and create new web browser controls when new tabs are being opened. Catch and cancel when the user opens new windows and open new tabs instead.

svinto
A: 

The following code works, just follow the first reply for creating the ExtendedWebBrowser class.

I'm using this to open a new tab but it also works to open a new window using your browser and not IE.

Hope it helps.

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (current_tab_count == 10) return;
        TabPage tabPage = new TabPage("Loading...");
        tabpages.Add(tabPage);
        tabControl1.TabPages.Add(tabPage);
        current_tab_count++;
        ExtendedWebBrowser browser = new ExtendedWebBrowser();
        InitializeBrowserEvents(browser);
        webpages.Add(browser);
        browser.Parent = tabPage;
        browser.Dock = DockStyle.Fill;
        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
        browser.DocumentTitleChanged += new EventHandler(Browser_DocumentTitleChanged);
        browser.Navigated += Browser_Navigated;
        browser.IsWebBrowserContextMenuEnabled = true;
public void InitializeBrowserEvents(ExtendedWebBrowser browser)
    {
        browser.NewWindow2 += new EventHandler<ExtendedWebBrowser.NewWindow2EventArgs>(Browser_NewWindow2);
    }

    void Browser_NewWindow2(object sender, ExtendedWebBrowser.NewWindow2EventArgs e)
    {

        if (current_tab_count == 10) return;
        TabPage tabPage = new TabPage("Loading...");
        tabpages.Add(tabPage);
        tabControl1.TabPages.Add(tabPage);
        current_tab_count++;
        ExtendedWebBrowser browser = new ExtendedWebBrowser();
        webpages.Add(browser);
        browser.Parent = tabPage;
        browser.Dock = DockStyle.Fill;
        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
        browser.DocumentTitleChanged += new EventHandler(Browser_DocumentTitleChanged);
        browser.Navigated += Browser_Navigated;
        tabControl1.SelectedTab = tabPage;
        browser.Navigate(textBox.Text);

        {
            e.PPDisp = browser.Application;
            InitializeBrowserEvents(browser); 
        }
Dev
Thank you very, VERY much Dev. The code works perfectly, you're awesome! Thanks so much mate. :D
baeltazor
A: 

it worked once for me, but then it just opens it in iexplorer

Fullmetal