|
-
Aug 19th, 2012, 01:32 PM
#1
Thread Starter
New Member
[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.
-
Aug 19th, 2012, 10:51 PM
#2
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.
-
Aug 20th, 2012, 04:54 AM
#3
Thread Starter
New Member
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.
-
Aug 20th, 2012, 09:59 AM
#4
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.
-
Aug 20th, 2012, 10:30 AM
#5
Thread Starter
New Member
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 did not work :/
Thanks in advance
-
Aug 20th, 2012, 10:52 AM
#6
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"
-
Aug 20th, 2012, 11:30 AM
#7
Thread Starter
New Member
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
-
Aug 20th, 2012, 01:08 PM
#8
Member
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! 
-
Aug 20th, 2012, 01:10 PM
#9
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.
-
Aug 20th, 2012, 01:10 PM
#10
Thread Starter
New Member
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.
-
Aug 20th, 2012, 01:23 PM
#11
Thread Starter
New Member
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 :/
-
Aug 20th, 2012, 01:48 PM
#12
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
-
Aug 20th, 2012, 02:01 PM
#13
Member
Re: VB 2012 Stop IE From Opening + New Tab
I may be new,but I've been that way for a long time! 
-
Aug 20th, 2012, 02:41 PM
#14
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|