Results 1 to 9 of 9

Thread: Prevent WebBrowser Control open external browser

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    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
    Check my Blog at VB Corner,Component Crafts

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    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?
    Check my Blog at VB Corner,Component Crafts

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    Re: Prevent WebBrowser Control open external browser

    @jmcilhinney. Ok heres my code

    vb Code:
    1. Public Class Form1
    2.     Dim _myURLIndex As Integer
    3.     Dim myURL As List(Of String)
    4.  
    5.     Public Function LoadFromDB() As List(Of String)
    6.         ' Here is a function that will load list of urls
    7.     End Function
    8.  
    9.     Public Sub New()
    10.  
    11.         ' This call is required by the designer.
    12.         InitializeComponent()
    13.  
    14.         ' Add any initialization after the InitializeComponent() call.
    15.  
    16.         myURL = LoadFromDB() 'Load url from database. From here, myURL will contain a list of URLs
    17.         _myURLIndex = 0
    18.         WebBrowser1.Navigate(myURL(_myURLIndex)) 'Navigate to first URL
    19.     End Sub
    20.  
    21.     Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    22.         If e.Url = WebBrowser1.Url Then
    23.             'If 1 website is finished loading, then go to next url from the list
    24.             If _myURLIndex < myURL.Count - 1 Then
    25.                 _myURLIndex += 1
    26.                 WebBrowser1.Navigate(myURL(_myURLIndex))
    27.             End If
    28.         End If
    29.     End Sub
    30. End Class
    Check my Blog at VB Corner,Component Crafts

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    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
    Check my Blog at VB Corner,Component Crafts

  8. #8
    Lively Member
    Join Date
    Jan 2007
    Posts
    96

    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Prevent WebBrowser Control open external browser

    Quote Originally Posted by youthbytes View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width