locked
Opening new window in VB browser...how to? RRS feed

  • Question

  • Hello guys!

    Im a newbie here, recently im programming PHP for web based approach and i've been using ready made browser like firefox as my application. But now im switching it to custom browser with visual basic. I've created my own custom browser and it seems ok. but my question here is, how to open up a new browser (new VB browser window) as same as we open a new browser in firefox. I've been using html code that opens new window, but it open up explorer browser instead and it took very long time to open. I want it to open up as VB browser with my php codes inside.

    Is there any html or php code that could open new vb browser window? instead of explorer or firefox window.

    thanks

    azhan
    Sunday, June 7, 2009 6:15 AM

Answers


  • I suppose the case is like-

    You've made a web browser in vb.net. You're using that browser for yourself. It works fine. However, in the page there's an anchor tag that has target attribute defined to open link in new window. Since we use WebBrowser Control in forms which is actually an Internet Explorer control, link like one I showed open them in new Internet Explorer. and I suppose you want them to open in your custom web browser.

    If so, I see 2 ways.

    1st is, grab the link, open new instance of application and pass this link as argument. For this your application must check for arguments when loading a form.

    2nd way is bit easy. Here you can create a new instance of webBrowser form and Show it. then you can directly set its webBrowser control to navigate to grabbed link.

    For 2nd way, you could use this-

        Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
            ' This will be triggered only when link tries to open in new window. 
            ' That means active element in web document will always be a Link.
    
            Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
            Dim target As String = myElement.GetAttribute("href")
    
            Dim newInstance As New Form1
            newInstance.Show()
            newInstance.WebBrowser1.Navigate(target)
    
            'cancel opening IE window
            e.Cancel = True
        End Sub


    And make sure your application's shutdown mode is set to Shutdown when Last Form Closes. Right Click your project name in solution manager and click Properties. Scroll Down and change the setting.



    Thanks

    - Omie
    You are so fat, the recursive function computing your fatness causes a stack overflow :\
    • Edited by Omie Sunday, June 7, 2009 7:47 AM formatting
    • Proposed as answer by Earl Tut Sunday, June 7, 2009 7:57 AM
    • Marked as answer by Martin Xie - MSFT Friday, June 12, 2009 8:56 AM
    Sunday, June 7, 2009 7:43 AM

All replies

  • A custom browser means for us something as a WebBrowser.

    Normally that is creating on a form two of them or using two forms where you place them on.

    Be aware that you have to overcome a lot of troubles why doing this, because the WebBrowser will only work mostly only like you think it works, while it is visible.

    However, it is here no advantages as you tell that you have used PHP, the only till you then mostly see that people start telling that this forum is not for ASPNET.

    Sunday, June 7, 2009 6:31 AM

  • I suppose the case is like-

    You've made a web browser in vb.net. You're using that browser for yourself. It works fine. However, in the page there's an anchor tag that has target attribute defined to open link in new window. Since we use WebBrowser Control in forms which is actually an Internet Explorer control, link like one I showed open them in new Internet Explorer. and I suppose you want them to open in your custom web browser.

    If so, I see 2 ways.

    1st is, grab the link, open new instance of application and pass this link as argument. For this your application must check for arguments when loading a form.

    2nd way is bit easy. Here you can create a new instance of webBrowser form and Show it. then you can directly set its webBrowser control to navigate to grabbed link.

    For 2nd way, you could use this-

        Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
            ' This will be triggered only when link tries to open in new window. 
            ' That means active element in web document will always be a Link.
    
            Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
            Dim target As String = myElement.GetAttribute("href")
    
            Dim newInstance As New Form1
            newInstance.Show()
            newInstance.WebBrowser1.Navigate(target)
    
            'cancel opening IE window
            e.Cancel = True
        End Sub


    And make sure your application's shutdown mode is set to Shutdown when Last Form Closes. Right Click your project name in solution manager and click Properties. Scroll Down and change the setting.



    Thanks

    - Omie
    You are so fat, the recursive function computing your fatness causes a stack overflow :\
    • Edited by Omie Sunday, June 7, 2009 7:47 AM formatting
    • Proposed as answer by Earl Tut Sunday, June 7, 2009 7:57 AM
    • Marked as answer by Martin Xie - MSFT Friday, June 12, 2009 8:56 AM
    Sunday, June 7, 2009 7:43 AM
  • Nice sample Omie - works great, thanks for sharing! :-)
    Sunday, June 7, 2009 7:57 AM
  • Does anybody know how to make this work in a tabbed web browser? I can't get it to work.
    Tuesday, September 28, 2010 11:48 PM
  • Does anybody know how to make this work in a tabbed web browser? I can't get it to work.


    show your code

    Also try this http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/13d3cfba-6227-438f-9d42-410b14ee0c8a

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Edited by kaymaf Wednesday, September 29, 2010 3:20 AM
    Wednesday, September 29, 2010 2:48 AM
  • I just want to know how can I use this code in my tabbed browser.

    --
    Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
            ' This will be triggered only when link tries to open in new window.
            ' That means active element in web document will always be a Link.

            Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
            Dim target As String = myElement.GetAttribute("href")

            Dim newInstance As New Form1
            newInstance.Show()
            newInstance.WebBrowser1.Navigate(target)

            'cancel opening IE window
            e.Cancel = True
        End Sub

    Wednesday, September 29, 2010 3:08 AM
  • I want to use this for TabControl, a web browser using TabControl. So, how can I use the above code for this? Plus, the information at the link did not work.
    Wednesday, September 29, 2010 4:06 AM
  • I did it:
        Private Sub wb_NewWindow(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles wb.NewWindow
            Dim myElement As HtmlElement = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Document.ActiveElement
            Dim target As String = myElement.GetAttribute("href")
    
            Dim newInstance As New Form3
            newInstance.Show()
            newInstance.wb.Navigate(target)
    
            'cancel opening IE window
            e.Cancel = True
        End Sub
    
        Private Sub OpenInNewWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenInNewWindowToolStripMenuItem.Click
            Dim myElement As HtmlElement = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Document.ActiveElement
            Dim target As String = myElement.GetAttribute("href")
    
            Dim newInstance As New Form3
            newInstance.Show()
            newInstance.wb.Navigate(target)
        End Sub
    
    wb is the name of my browser control
    It's pronounced Sky - Fi, and its gonna rule the world,cos i'm inventing it
    Monday, October 3, 2011 6:35 PM
  • I am also trying to figure this out in a tabcontrol environment, as I'm using 

     

       Private Sub WebBrowser_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    
            Dim myElement As HtmlElement = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Document.ActiveElement 'Get the new window's destination
            Dim genstr = myElement.GetAttribute("href")
            MessageBox.Show(genstr)
            AddTab(genstr, TabControl1)
    
            e.Cancel = True
        End Sub

    its canceling the new window but not grabbing the link, it opens a blank tab as genstr return nada

    Monday, October 3, 2011 9:01 PM
  • Thank you very much it was helpfull
    Friday, April 20, 2012 11:03 PM
  • This isn't working for me. It's still opening IE instead of a new instance of my app. I am using WebBrowser to navigate directories/files on my PC rather than actual web pages. What I want is to have a new instance of my app open when I right click on a folder and choose "Open in new window" from WebBrowser's built-in context menu.

    http://amerigoware.myfreeforum.org/

    Tuesday, August 14, 2012 1:56 PM
  • I am using the 2nd for a long time in my custom web browser but it doesn't work always. Often, when a link is clicked, a new window opens but navigates to about:blank or it shows the message "Navigation to the webpage was cancelled". This happens in certain webpages, e.g. Google News and Microsoft Sign in Window.

    Is there a solution for this?

    And sth else: Setting the app shutdown mode when last form closes won't make the app close all windows? For instance, if you want to close the new window and return to the main one, will it be possible?


    Kostopoulos Konstantinos

    Saturday, July 13, 2013 11:43 AM