Results 1 to 27 of 27

Thread: [RESOLVED] Find Clicked Url Link/ Open Link In New Tab

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Resolved [RESOLVED] Find Clicked Url Link/ Open Link In New Tab

    Hi All,

    Im trying to create a webbrowser in VB 08;

    I was wondering how i would create new tab with the Link the clicked.

    *EG* They Right-Click Link > Open In New Window/Tab > Makes New Tab In My Program > Navigates To The Linked Clicked.

    Is there any possible way of doing this?!

    Thanks In Advance,

    Jamie.

  2. #2
    Addicted Member Gonegaming12's Avatar
    Join Date
    Jun 2006
    Location
    USA
    Posts
    131

    Re: Find Clicked Url Link/ Open Link In New Tab

    Use a TabControl and when the url is clicked add a new tab and generate a new WebBrowser control in it.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    Quote Originally Posted by Gonegaming12 View Post
    Use a TabControl and when the url is clicked add a new tab and generate a new WebBrowser control in it.
    Done That, But How Would I get it to navigate to the link clicked url?

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Find Clicked Url Link/ Open Link In New Tab

    Set the WebBrowser.Nagivate("siteClicked")
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    Quote Originally Posted by danasegarane View Post
    Set the WebBrowser.Nagivate("siteClicked")
    Thats The thing, How do i get it to find the link they clicked on ? From The Website They was on.

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    The webbrowser control raises a number of events when the user tries to do things such as following a hyperlink.

    I havent got visual studio open at the moment but if you look at the events it raises you ought to be able to trap the appropriate event and examine the URL it is expecting to navigate to. Just intercept this and get your new browser to navigate to that URL

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    Quote Originally Posted by keystone_paul View Post
    The webbrowser control raises a number of events when the user tries to do things such as following a hyperlink.

    I havent got visual studio open at the moment but if you look at the events it raises you ought to be able to trap the appropriate event and examine the URL it is expecting to navigate to. Just intercept this and get your new browser to navigate to that URL
    Sorry To be off hassle but i dont know what the event is. and how i would set it out.

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    OK the webbrowser control has a "Navigating" event. Like all standard framework events this passes an eventargs object - in this case its a specialised event args object : WebBrowserNavigatingEventArgs, which among other properties it exposes is "URL" telling you where it is about to navigate to.

    This event gets fired before the webbrowser control actually starts navigating, so you can put code into this event, trap the URL that it is about to navigate to, create your new tab with a new webbrowser control and navigate that browser to this URL, and cancel navigation on the initial webbrowser.

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    Please could u give me the code ( i cant seem to figure it out ) this is my newWindow Code:

    Code:
            Dim browse As New WebBrowser
            TabControl1.TabPages.Add(i, "Page" & 1)
            browse.Name = "wb"
            browse.Dock = DockStyle.Fill
            TabControl1.SelectTab(i)
            TabControl1.SelectedTab.Controls.Add(browse)
            i = i + 1
            e.Cancel = True
    I have got it to cancel the internet explorer to open but cant get/set the url.

  10. #10
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    How is this code called? ie is it being called from an event and if so what event?

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow

  12. #12
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    OK the NewWindow event doesn't reveal the URL so you'd need to get the URL from a different event. Here's an example which grabs the URL you are about to navigate to from the Navigating method and then displays that in the NewWindow Event :

    Code:
    Public Class Form1
    
        Private _URL As String
    
        Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    
            _URL = e.Url.ToString
    
        End Sub
    
        Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
    
            MessageBox.Show(_URL)
    
        End Sub
    
    
    End Class
    So you ought to be able to combine that code with your own code to use _URL to navigate your new browser.

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    would this by any chance work:

    Code:
        Dim i As Integer = 1
        Public _URL As String
        Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    
            _URL = e.Url.ToString
    
        End Sub
        Public Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
            Dim browse As New WebBrowser
            TabControl1.TabPages.Add(i, "Page" & 1)
            browse.Name = "wb"
            browse.Dock = DockStyle.Fill
            browse.ScriptErrorsSuppressed = True
            TabControl1.SelectTab(i)
            TabControl1.SelectedTab.Controls.Add(browse)
            e.Cancel = True
            AddHandler WebBrowser1.Navigating, AddressOf WebBrowser1_Navigating
            AddHandler WebBrowser1.DocumentCompleted, AddressOf browse_done
            browse.Navigate(_URL)
            i = i + 1
        End Sub

  14. #14
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    Looks good at first glance but the best way to find out is to try it...

    (NB not sure why you've made _URL public - if you want to expose it as a property you should give it a "proper" name such as URL)

  15. #15

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    For some reason it doesnt. it just displays a white page.

  16. #16
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    Quote Originally Posted by Jamie2993 View Post
    For some reason it doesnt. it just displays a white page.
    OK a couple of things.

    I'd put a breakpoint on the line browse.navigate(_URL) and check that a) the line is being run and b) check what _URL evaluates to.

    EDIT - also, when you are adding handlers you don't seem to he adding to the new browser control.

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    right it does get the link + what do u mean adding to the new browser control?

  18. #18
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    You are creating a new browser control with this line

    Code:
    Dim browse As New WebBrowser
    These two lines are adding event handlers dynamically

    Code:
    AddHandler WebBrowser1.Navigating, AddressOf WebBrowser1_Navigating
    AddHandler WebBrowser1.DocumentCompleted, AddressOf browse_done
    Which is all well and good, but you are adding event handlers to WebBrowser1 (which I'm guessing is the default webbrowser on the 1st tab?) and not your new webbrowser. If you want to add the handlers to your new WebBrowser then I'd expect it to be

    Code:
    AddHandler browse.Navigating, AddressOf WebBrowser1_Navigating
    AddHandler browse.DocumentCompleted, AddressOf browse_done

  19. #19

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    good point, ill try.

  20. #20

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    right that does work but when u try do that on the tab u just made it just open it in Internet explorer.

  21. #21
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    OK... lets take a step back...

    how does the user activate this functionality? when they right-click on a link are you implementing your own popup menu or are you using the browser's built in popup menu?

  22. #22

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    the built in one. + thats extremly weird, i havent changed nothing and its stopped working

  23. #23

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    ok well im off to bed, due to me having work in the morning, but please could u help as much as possible while im gone.

  24. #24
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    Quote Originally Posted by Jamie2993 View Post
    the built in one. + thats extremly weird, i havent changed nothing and its stopped working
    Does the initial browser (the one created at design time) work differently from the new (dynamically created) one?

  25. #25
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Find Clicked Url Link/ Open Link In New Tab

    Quote Originally Posted by Jamie2993 View Post
    ok well im off to bed, due to me having work in the morning, but please could u help as much as possible while im gone.
    Fraid not - I too have work in the morning (and a 100 mile trip to get to work) - while I'm happy to help you identify where your code isn't workign, I'm not going to spend the night beavering away at your problem while you snooze!

  26. #26

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    well im finally back lol, lets get this thread showing again

  27. #27

    Thread Starter
    Member
    Join Date
    Aug 2009
    Location
    England
    Posts
    38

    Re: Find Clicked Url Link/ Open Link In New Tab

    right ive got it to add a new tab + navigate to A url but the url is the tab u just selected the link from
    E.G on tab at a page for a google search "visual basic" u right-click on a link > new window > it will navigate to the google search page.

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