Hi,
How do you get the WebBrowser's Url in a Combobox? I've tried the Url member but it says "Value of type 'System.Windows.Forms.WebBrowser' cannot be converted to 'String'." I also tried the LocationURL member also but it was in VB6 and not VB.NET. Please help me. Thanks in advance

Dani AI

Generated

Short answer: the WebBrowser control exposes a System.Uri (not a plain string), so update the ComboBox after navigation and convert the Uri to the form you want. was on the right track for storing URLs in the list and pointed toward putting the browser address into the ComboBox’s visible text — the following expands that into a robust, production-ready approach.

Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
    If e.Url Is Nothing Then Exit Sub

    ' pick a string form: file paths vs http(s)
    Dim urlText As String = If(e.Url.IsFile, e.Url.LocalPath, e.Url.AbsoluteUri)

    ' ensure UI update happens on the UI thread
    If ComboBox1.InvokeRequired Then
        ComboBox1.Invoke(Sub()
                             ComboBox1.Text = urlText
                             If Not ComboBox1.Items.Contains(urlText) Then ComboBox1.Items.Insert(0, urlText)
                         End Sub)
    Else
        ComboBox1.Text = urlText
        If Not ComboBox1.Items.Contains(urlText) Then ComboBox1.Items.Insert(0, urlText)
    End If
End Sub

Notes and cautions: DocumentCompleted can fire multiple times for frames — if you use that event check e.Url against the control’s main Url to avoid per-frame repeats. Use OriginalString if you need the exact text the browser requested, AbsoluteUri for a normalized form, and LocalPath for file URIs. Always null-check the Uri before converting to text to avoid exceptions, and perform UI updates on the UI thread (Invoke/BeginInvoke) if your handler can be called off the UI thread. For history/MRU behavior, avoid duplicates (Items.Contains) and consider persisting the list with application settings or using the ComboBox AutoCompleteCustomSource for a nicer UX.

This preserves the helpful ideas from and while handling real-world edge cases and threading.

Recommended Answers

All 5 Replies

ComboBox1.Items.Add(WebBrowser1.Url.ToString)

Hi artemix22,
I wanted to have the combobox to have the text of the webbrowser's url and not to add it to the combobox.

try Combobox1.Text = webBrowser1.Url.ToString()

Wow!
Thank You vey much for your contribution artemix22 and tinsaafl.

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.