Ok I have a program that has some web controls. When the user logs into the web system it loads in WebBrowser4 and their worklist comes up. I want to code the web control so if they click any link in the control it will load those links in a new IE window.

Any idea how I can go about this?

Trap the "main" webbrowser control's Navigating event to obtain the target url and to cancel the navigation in the "main" webbrowser.

' Use this flag to indicate initial loading of the first web control
  Private _InitialLoad As Boolean

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' 
    _InitialLoad = True
    ' I used google as an example
    WebBrowser1.Navigate("http://www.google.com")
    _InitialLoad = False

  End Sub
  
  Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    ' Grab the URL and cancel navigation
    Dim NewURI As System.Uri

    ' Exit if we are loading the first web page
    If _InitialLoad Then Exit Sub

    ' This is a link (URL) that user clicked in the google's page
    NewURI = e.Url
    ' Cancel WebBrowser1 to navigate away from the initial page
    e.Cancel = True
    ' WebBrowser2 navigates to URL instead of WebBrowser1
    WebBrowser2.Navigate(NewURI)

  End Sub

There's a flag to prevent navigation cancelling during the initial page load. Initial page load is done in the form's load event but can be done some other way too. This is just to show the idea.

HTH

I'll give this a try, thanks, but actually I want any new links clicked in the web control to open a new IE window and not load in web control.

I see. I wasn't sure if you meant to start the browser or the browser control.

So here's a modified version that opens the links in IE (plus one minor bug fixed).

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' 
    _InitialLoad = True
    WebBrowser1.Navigate("http://www.google.com")
    ' FIX: Do not reset _InitialLoad in here, wait for DocumentCompleted event
    '_InitialLoad = False

  End Sub

  Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    ' Reset _InitialLoad after the WebBrowser control has _finished_ loading

    _InitialLoad = False

  End Sub

  Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    ' Grab the URL and cancel navigation
    Dim NewURI As System.Uri

    ' Exit if we are loading the first web page
    If _InitialLoad Then Exit Sub

    NewURI = e.Url
    e.Cancel = True
    ' WebBrowser2 navigates to URL
    ' WebBrowser2.Navigate(NewURI)

    ' MOD: Start IE with the URL
    Dim IEprocess As Process = New Process()

    IEprocess.StartInfo.Arguments = NewURI.ToString()
    IEprocess.StartInfo.FileName = "iexplore.exe" ' Assuming IE is found in the Path
    IEprocess.StartInfo.UseShellExecute = True
    IEprocess.StartInfo.WindowStyle = ProcessWindowStyle.Normal

    If Not IEprocess.Start() Then
      MessageBox.Show("Could not start IE", "IE Launch Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

  End Sub

Well, it looks fairly good but now the program starts loading IE windows as soon as it's launched and it only should when user actually clicks a link in WebBrowser4 control

My bad, maybe I am not explaining enough. I have a program that contains a web control and a combo box. The combo box contains a list of servers to log into. When one is selected, it loads in WebBrowser4 web control. User logs in and have a worklist to work from. What I am trying to do is program this so once they are in worklist and click a link, I want that clicked link to be fired off in a new, IE window, not in the WebBrowser4 web control. I want the web control to maintain the worklist page that is already up.

Ok, I think I got the idea. Basically all the "elements" were already there, let's just add a combobox to choose from the webcontrol content.

' Use this flag to indicate initial loading of the first web control
  Private _InitialLoad As Boolean

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' 

    ' Fill the combo box with servers
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    ComboBox1.Items.Clear()
    ComboBox1.Items.Add("http://www.google.com")
    ComboBox1.Items.Add("http://www.yahoo.com")
    
    ' You can also use DisplayMember etc. properties to make this list a bit prettier

  End Sub
  
   Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    ' Reset _InitialLoad after the WebBrowser control has _finished_ loading

    _InitialLoad = False

  End Sub

  Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    ' Grab the URL and cancel navigation
    Dim NewURI As System.Uri

    ' Exit if we are loading the first web page
    If _InitialLoad Then Exit Sub

    NewURI = e.Url
    e.Cancel = True

    Dim IEprocess As Process = New Process()

    IEprocess.StartInfo.Arguments = NewURI.ToString()
    IEprocess.StartInfo.FileName = "iexplore.exe" ' Assuming IE is found in the Path
    IEprocess.StartInfo.UseShellExecute = True
    IEprocess.StartInfo.WindowStyle = ProcessWindowStyle.Normal

    If Not IEprocess.Start() Then
      MessageBox.Show("Could not start IE", "IE", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

  End Sub

  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    ' Change the server

    ' Prevent opening IE at this point
    _InitialLoad = True
    ' Load login page
    WebBrowser1.Navigate(ComboBox1.SelectedItem.ToString())

  End Sub

And again the point is to use a flag (_InitialLoad) to prevent opening IE too early.

HTH

Thanks for your help Teme64, I added all your code but it behaves the same way it did without it. Still opens the links in the web control instead of IE. I did just stumble upon something. The important links are a type of query so I got it to work by doing this..

[IEprocess.StartInfo.Arguments = ("http://blahblah.com/servlet/com.test.servlet.util.studyQuery?")

The only problem is I have to modify the url grabber so it will get the correct first half of the url string as each server is different.

Edited 6 Years Ago by wb4whd: n/a

ok, so I made a string that holds the first part of the url which is unique for each server. When that server is selected, the string is populated at the same time as the combo box selection to pass to web control 4. I then join the first part of the url with a second string which is the actual query and is the same for all servers. For some reason the query only works for one server, all the others revert back to loading the page in the web control even though all are coded the same?

I don't know maybe this won't work at all. It worked at first for one server, now won't work for any.

This article has been dead for over six months. Start a new discussion instead.