Question Open Default web browser

Krenar

New member
Joined
Feb 20, 2014
Messages
1
Programming Experience
1-3
What im trying to do is to open my default web browser from a button and to search for google. And in google search box to type what im searching from textbox in my form and to click the search buton in my default browser.

im using this code

VB.NET:
Dim url As String
        Dim browser As String = "www.google.com"
        url = browser

        Try
            If Not (browser = "www.google.com") Then
                Try
                    Process.Start(browser, url)
                    


                Catch ex As Exception
                    Process.Start(url)



                End Try
            Else
                Process.Start(url)
            End If
        Catch ex As Exception

           

        End Try
 
Firstly, it doesn't make sense to have the 'browser' and the 'url' variables both contain "www.google.com". That 'browser' variable doesn't seem to make sense at all because it contains a URL, not a browser. Is it supposed to be there to allow the user to select a browser other than the default? If so then it's certainly not doing anything of the sort right now.

Anyway, you said that you want to open the default browser so let's concentrate on that. To do that you simply call Process.Start and pass the URL that you want to display. In the case of display Google though, it doesn't make sense to open the home page, then somehow enter some text to search for and then somehow click the Search button. While that's possible it is no fun and it's not necessary. If you just include the text in the original URL then you can go straight to the results page, e.g.
Process.Start("www.google.com/search?q=" & HttpUtility.UrlEncode(TextBox1.Text))
Just note that the HttpUtility class is defined in the System.Web assembly, so you'll need to reference that.

If you did want to use a different browser then you would get a list of installed browsers from the Registry and let the user selected one, so you'd know that it would be valid. You'd then pass the path of the browser executable as the first argument and the URL as the second.
 
Back
Top