|
-
Apr 14th, 2012, 12:14 AM
#1
Thread Starter
Addicted Member
Prevent WebBrowser Control open external browser
Hi,
I have a windows form that have 5 webbrowser control. I also have serveral links of URLs and each webbrowser will open them. My problem is that after all links already opened, when I close my windows forms, sometimes my application suddenly open my default browser automatically and open url that on my URL list. I already tried to make all webbrowser to navigate to "about:blank"to make sure there are not loading any webpage before I close my application but the problem is still occur. This has given me a headache since this problem is not always occur when I close my application.
Any help will be very appreciated
Regards,
Michael
-
Apr 14th, 2012, 12:31 AM
#2
Re: Prevent WebBrowser Control open external browser
The WebBrowser control will not spontaneously open a new window. Unless something is corrupt, the only way that will happen is if a link is clicked, either physically or in code, or else you call Navigate in code. We can only guess at what might be the issue in your case.
-
Apr 14th, 2012, 01:00 AM
#3
Thread Starter
Addicted Member
Re: Prevent WebBrowser Control open external browser
Yes, I called WebBrowser.Navigate for each URL list. But even if 1 url is not fully loaded when I started to called Navigate for 2nd url, its going fine. No external browser opened. The external browser is opened after I close the form. Is there anyway to prevent this?
-
Apr 14th, 2012, 03:03 AM
#4
Re: Prevent WebBrowser Control open external browser
You seem to be missing the point. That external web browser window will not simply open spontaneously. It is being opened because your app is opening it. The way to prevent it is to not do whatever it is that you're doing to cause it in the first place. We can only guess what that might be. Maybe if you were to show us some code we might be able to more than guess.
-
Apr 14th, 2012, 05:24 AM
#5
Thread Starter
Addicted Member
Re: Prevent WebBrowser Control open external browser
@jmcilhinney. Ok heres my code
vb Code:
Public Class Form1 Dim _myURLIndex As Integer Dim myURL As List(Of String) Public Function LoadFromDB() As List(Of String) ' Here is a function that will load list of urls End Function Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. myURL = LoadFromDB() 'Load url from database. From here, myURL will contain a list of URLs _myURLIndex = 0 WebBrowser1.Navigate(myURL(_myURLIndex)) 'Navigate to first URL End Sub Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted If e.Url = WebBrowser1.Url Then 'If 1 website is finished loading, then go to next url from the list If _myURLIndex < myURL.Count - 1 Then _myURLIndex += 1 WebBrowser1.Navigate(myURL(_myURLIndex)) End If End If End Sub End Class
-
Apr 14th, 2012, 08:28 AM
#6
Re: Prevent WebBrowser Control open external browser
All that extra code in your constructor should be in the Load event handler. You should never have data access code in a constructor.
Apart from that, I see that you have a call to Navigate in the DocumentCompleted event handler. After my telling you multiple times that it would be your code causing this window to open, have you tried checking whether it's that line doing it?
-
Apr 14th, 2012, 11:49 PM
#7
Thread Starter
Addicted Member
Re: Prevent WebBrowser Control open external browser
My thoughts is that when an url finished loading, I want that webbrowser to navigate to next URL. Is there something wrong with my code?
I have checked some of the URLs, and it fires DocumentCompleted event after they finished loading. I can't check all URLs since its so many. I will have about 100 random URLs to check
-
Oct 27th, 2014, 02:50 PM
#8
Lively Member
Re: Prevent WebBrowser Control open external browser
I'm actually having a similar issue when navigating to https://www.namecheap.com/myaccount/login.aspx (all I'm doing presently is just navigating there so it's def nothing in my code causing the problem).
For some reason it will shell a new instance of default browser (or a new tab in default browser if it's already open), but the NewWindow event never gets fired. I'm super confused right now and haven't used a WB control in years.
Can someone shed some light here? I'm pretty lost. lol.
-
Oct 27th, 2014, 08:48 PM
#9
Re: Prevent WebBrowser Control open external browser
 Originally Posted by youthbytes
I'm actually having a similar issue when navigating to https://www.namecheap.com/myaccount/login.aspx (all I'm doing presently is just navigating there so it's def nothing in my code causing the problem).
For some reason it will shell a new instance of default browser (or a new tab in default browser if it's already open), but the NewWindow event never gets fired. I'm super confused right now and haven't used a WB control in years.
Can someone shed some light here? I'm pretty lost. lol.
I'm not sure why they chose to do it this way but one of the weaknesses of the .NET WebBrowser control is that the NewWindow event doesn't get raised under all circumstances in which a new window gets opened. The control is a wrapper around the same ActiveX web browser control that is used in VB6 but it doesn't expose all the functionality of that control. That ActiveX control has a number of events related to new windows being opened that do not map to the NewWindow event of the WebBrowser control.
You can get a reference to the underlying ActiveX control via the ActiveXInstance property and handle those extra events. It's not completely straightforward but it is possible. I've always meant to do it myself so that this question doesn't need to be answered over and over but never quite got around to it as I've never needed it myself.
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
|