DrewNaylor / DrewWebBrowser

A lightweight web browser I'm working on based on the VB.Net Windows Forms WebBrowser control. Not in development until further notice as of January 1, 2018.
GNU General Public License v3.0
4 stars 3 forks source link

Allow links to be opened in a new tab. (link to code provided.) #8

Open DrewNaylor opened 7 years ago

DrewNaylor commented 7 years ago

I don't know if this will be easy to do considering the things IE already provides in the context menu, but putting this button in the context menu will require me to make one.

https://social.msdn.microsoft.com/Forums/en-US/4204c991-b15e-4968-a37a-392251808ca8/vs-2010-opening-links-in-a-new-tab-code-for-a-tabbed-web-browser?forum=vblanguage

Hi,

Here is my walkthrough how to open links in new tab within webbrowser control.

1- Place and dock tabcontrol on your form.

2- Place and dock a webbrowser control inside tab control's tabpage1. Make Webbrowser's name "mywb" which is on tabpage1.

3- Set Webbrowser's IsWebBrowserContextMenuEnabled to False.

4- Add a ContextMenuStrip control on your form which will be added named "ContextMenuStrip1" , then in Webbrowser control's properties set this ContextMenuStrip1 as WebBrowser control's ContextMenuStrip.

5- Add item to ContextMenuStript like "Open In New Tab" , then finally, handle OpenInNewTabToolStripMenuItem 's Click event by double clicking "Open In New Tab" Item that is in your ContextMenuStript control like this:
Private Sub OpenInNewTabToolStripMenuItem_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles OpenInNewTabToolStripMenuItem.Click
        ' create a new webbrowser instance on new tab
        Dim wb As New WebBrowser
        wb.Name = "mywb"
        wb.Url = _
New Uri(DirectCast(Me.TabControl1.SelectedTab.Controls("mywb"), _ WebBrowser).Document.ActiveElement.GetAttribute("href"))
        wb.Dock = DockStyle.Fill
        ' re associate contextmenustrip for each webbrowser instance
        wb.ContextMenuStrip = Me.ContextMenuStrip1
        wb.IsWebBrowserContextMenuEnabled = False
        Dim tabpage As New TabPage("This is New Tab")
        tabpage.Controls.Add(wb)
        Me.TabControl1.TabPages.Add(tabpage)

    End Sub
Tested the code which i've written, runs fine.

Hope this helps.

Saygılarımla, Onur Güzel