I'm a beginner and like to know how do you get all links in browser1 and display them in a listbox

Dani AI

Generated

Two practical options, depending on the goal: use an embedded browser control so you capture links after the page has rendered (including those created by JavaScript), or fetch the page HTML and parse it server-side (faster and simpler when no script-generated links are needed). described the raw-HTML route; showed a browser/DOM route. The advice below expands on the browser approach and gives concrete steps for a WinForms VB.NET workflow, plus common pitfalls to avoid.

Use the WebBrowser control and handle its DocumentCompleted event (do not rely on busy-wait/DoEvents). The example below collects anchor hrefs, normalizes relative URLs to absolute, skips non-HTTP schemes, and avoids duplicates:

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If e.Url <> WebBrowser1.Url Then Return

    ListBox1.Items.Clear()
    Dim baseUri As Uri = WebBrowser1.Url
    Dim seen As New System.Collections.Generic.HashSet(Of String)(StringComparer.OrdinalIgnoreCase)

    For Each el As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
        Dim href As String = el.GetAttribute("href")
        If String.IsNullOrEmpty(href) Then Continue For
        If href.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase) OrElse href.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase) Then Continue For

        Dim absolute As String
        Try
            absolute = (New Uri(baseUri, href)).AbsoluteUri
        Catch
            absolute = href
        End Try

        If seen.Add(absolute) Then ListBox1.Items.Add(absolute)
    Next
End Sub

Troubleshooting and extras:

  • Some sites load links after the initial load (AJAX). If links are missing, wait for a specific DOM element or use a headless browser (Selenium/CefSharp) that executes JS.
  • Multi-frame pages fire DocumentCompleted multiple times; the e.Url check above ensures you act on the top-level document.
  • If you need faster, non-rendered extraction, fetch HTML with HttpClient and use a proper parser (HtmlAgilityPack) — avoid regex on HTML.
  • In VB6/MSHTML scenarios (as mentioned by ), prefer the DocumentComplete event and query anchor elements for their href property to get usable URLs.

Checklist: decide render vs. fetch, wait for final document, normalize URLs, filter schemes, and deduplicate before showing results.

Recommended Answers

All 2 Replies

Hi,

Toget link from the Webpage you have to parse the Htmlcode of the webpage(you need to download the webpage first)

i assumed that you have the HTML code in a text box, then simly look for the tag <a href>
The links usually provided in this tag only, the text proceeding after "=" symbol and between the double quates is the link content, store the link in a array, continue further for collecting links in the webpage.

After creating the array , load the array elements to you list/Combobox.


Hope this will help you,


With regards
Venkatramasamy SN

On a blank form, add the following controls

1. Listbox
2. WebBrowser Control
3. Command Button

Add a project reference to "Microsoft HTML Object Library"

Private Sub Command1_Click()
    Dim oHTMLDoc As MSHTML.HTMLDocument
    Dim oHTMLElement As MSHTML.IHTMLElement
    
    WebBrowser1.navigate "http://www.google.com/"

    Do
        DoEvents
    Loop Until WebBrowser1.readyState = READYSTATE_COMPLETE
    
    Set oHTMLDoc = WebBrowser1.document
    
    For Each oHTMLElement In oHTMLDoc.links
        List1.AddItem oHTMLElement
    Next
    
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.