Results 1 to 14 of 14

Thread: [RESOLVED] VB 2012 Stop IE From Opening + New Tab (Tabbed Browser)

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    13

    Resolved [RESOLVED] VB 2012 Stop IE From Opening + New Tab (Tabbed Browser)

    Hey,

    So I have a tab control that opens new tabs once I click a button. Now the problems is that, when something opens in a new window, IE opens up. So I want it to; instead of opening in a new window, to open in a new tab (In my Browser) or at least in a new window of my browser.

    Here is the code to adding a new tab:
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim Browser As New WebBrowser
            TabControl1.TabPages.Add("New Page")
            TabControl1.SelectTab(int)
            Browser.Name = "Web Browser"
            Browser.Dock = DockStyle.Fill
            TabControl1.SelectedTab.Controls.Add(Browser)
            AddHandler Browser.DocumentCompleted, AddressOf Done
            int = int + 1
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoHome()
        End Sub
    Thanks in advance
    Last edited by Paul Shane; Aug 20th, 2012 at 02:42 PM.

  2. #2
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: VB 2012 Stop IE From Opening + New Tab

    Capture the newwindow event and open up a new tab:

    Code:
    Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
            e.Cancel = True
            'Open up a new tab and navigate to the new page
            'Or just renavigate to the new page in the same webbrowser
            Call newTab()
    
        End Sub
    
        Private Sub newTab()
            TabControl1.TabPages.Add("New Tab")
    
            Dim wb As New WebBrowser
            wb.Dock = DockStyle.Fill
            TabControl1.TabPages(TabControl1.TabPages.Count - 1).Controls.Add(wb)
        End Sub
    Kinda like that.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    13

    Re: VB 2012 Stop IE From Opening + New Tab

    Thanks for your replay, but I still get an error where there is this code: Handles WebBrowser1.NewWindows

    The Problem is that I don't have a webbrowser1.

    Can anyone help please.

  4. #4
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: VB 2012 Stop IE From Opening + New Tab

    You've a dynamic webbrowser that you've allready created a handler for the document completed, I would suggest that you add a handler for the NewWindow event and do whatever code you need to(see example in post #2) in that sub.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  5. #5
    New Member
    Join Date
    Aug 12
    Posts
    13

    Re: VB 2012 Stop IE From Opening + New Tab

    Ok I have managed to get this far:

    I added the handler in form1 load

    Form1_Load:
    Code:
    AddHandler Browser.NewWindow, AddressOf BrowserNewWindow
    Opens In New Tab:
    Code:
    Private Sub BrowserNewWindow(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim NewURL As String = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).StatusText
            Dim Browser As New WebBrowser
            AddHandler Browser.NewWindow, AddressOf BrowserNewWindow
            TabControl1.TabPages.Add("New Page")
            TabControl1.SelectTab(int)
            Browser.Name = "Web Browser"
            Browser.Dock = DockStyle.Fill
            TabControl1.SelectedTab.Controls.Add(Browser)
            AddHandler Browser.DocumentCompleted, AddressOf Done
            int = int + 1
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(NewURL)
        End Sub
    I works, but the only problem with this is that, IE still opens up as the
    Code:
    e.Cancel = True
    did not work :/

    Thanks in advance

  6. #6
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: VB 2012 Stop IE From Opening + New Tab

    A couple of things that I noticed was that you're adding 2 of the same handlers. One is the one being added in the form_load while the other is being added in the handler made in the form_load event. Take out everything in the NewWindow event except for what you want to happen, like:
    1. Adding the new tab
    2. Adding a new instance of "Browser" to the new tab
    3. Navigating to the desired webpage in the new "Browser"
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  7. #7
    New Member
    Join Date
    Aug 12
    Posts
    13

    Re: VB 2012 Stop IE From Opening + New Tab

    Ok here are my problems:

    1. When the new windows opens, it creates a new tab, but opens the link in the previous tab.
    2. IE still opens when a new window opens.

    Now what I want is:

    1. Open the link in a new tab, not the previous, when a new window opens.
    2. Stop IE from opening

    I have no idea how to code this, so please help.

    Thanks

  8. #8
    Member
    Join Date
    Jul 12
    Posts
    41

    Re: VB 2012 Stop IE From Opening + New Tab

    I think its because IE is set as your default browser!
    I may be new,but I've been that way for a long time!

  9. #9
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: VB 2012 Stop IE From Opening + New Tab

    I think its because IE is set as your default browser!
    Nope, it's because he isn't handling the NewWindow event with the correct webbrowser.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  10. #10
    New Member
    Join Date
    Aug 12
    Posts
    13

    Re: VB 2012 Stop IE From Opening + New Tab

    No actually google chrome is my default web browser, so I don't think that's the case. Visual Studio uses IE as it's default I think. I am really new to Visual Basic.

  11. #11
    New Member
    Join Date
    Aug 12
    Posts
    13

    Re: VB 2012 Stop IE From Opening + New Tab

    Can you help me with the code. I know you did, but I just don't get it :/

  12. #12
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: VB 2012 Stop IE From Opening + New Tab

    Take a look at this:
    Code:
    'All ways turn strict/explicit on
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        'Declare the webbrowser and a url string
        Private browser As WebBrowser
        Private urlString As String = "www.vbforums.com"
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Calls the sub "newTab"
            newTab()
        End Sub
    
        Private Sub newTab()
            'Creates a new instance of the webbrowser
            browser = New WebBrowser
    
            'Sets the webbrowser's properties
            With browser
                .Navigate(urlString)
                .Dock = DockStyle.Fill
            End With
    
            'Declare a new tabpage and add it to the tabcontrol
            Dim tp As New TabPage("Tab Number " & TabControl1.TabCount + 1)
            TabControl1.Controls.Add(tp)
    
            'Add the webbrowser to the tabpage
            tp.Controls.Add(browser)
    
            'Create a new instance of the NewWindow for the new webbrowser
            AddHandler browser.NewWindow, AddressOf BrowserNewWindow
        End Sub
    
        Private Sub BrowserNewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs)
            'Stops the new window
            e.Cancel = True
    
            urlString = "www.google.com" 'Or whatever
    
            'Calls the sub "newTab"
            newTab()
        End Sub
    
    End Class
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  13. #13
    Member
    Join Date
    Jul 12
    Posts
    41

    Re: VB 2012 Stop IE From Opening + New Tab

    Very good!
    I may be new,but I've been that way for a long time!

  14. #14
    New Member
    Join Date
    Aug 12
    Posts
    13

    Re: VB 2012 Stop IE From Opening + New Tab

    Thanks dday9, it worked perfectly. I arranged it so it goes to the proper URL instead of Google. So, here is the code if anyone else if interested.

    Code:
    Private Sub BrowserNewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs)
    'Stops the new window
            e.Cancel = True
    
             Dim element As HtmlElement = Browser.Document.ActiveElement
            Dim url As String = element.GetAttribute("href")
            urlString = url
    
            'Calls the sub "newTab"
            newTab()
    End Sub
    The rest of the code that dday9 gave rests the same.

    Thanks once again. Ill mark this thread as solved
    Last edited by Paul Shane; Aug 20th, 2012 at 02:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •