How to Create a Web Browser in Visual Basics 2008

104 6
    • 1). Add a TextBox to the top of your form. Name it "AddressBar" and change it's text to "http://www.google.com" or any other default web address you'd like to use.

    • 2). Add a Button next to the textbox. Name it "GoButton" and change it's text property to "GO."

    • 3). Add the "WebBrowser" control to the components list and drag its borders so that it fills the rest of the window. Go to the properties list on the bottom right hand side of the screen and find the "Anchor" entry. It will read "Top, Bottom." Click the drop down box to change it to "Top, Bottom, Left, Right." This will ensure that your web browser control always fills the window if the user resizes it.

      Next, go to the Url property of the web browser control and change it to read "http://www.google.com." If you chose another default website in step 1, use that instead.

    • 4). Double click the "GoButton" you created in step 2. This will create an event function named "GoButton_Click" and take you to it automatically. Paste the following code into it. The REM lines describe what the code is doing on each line:

      REM Set aside some memory to hold a new URL.
      Dim url As Uri

      REM Try to create a URL from the text in the address bar.
      Try
      url = New Uri(AddressBar.Text)

      REM If the text in the address bar does not make sense as a URL, give the user warning
      REM and then tell them that you are going to Google instead.
      Catch ex As Exception
      MsgBox("Invalid URL. Going to Google instead.")
      url = New Uri("http://www.google.com")
      AddressBar.Text = "http://www.google.com"

      End Try

      REM Tell the web browser component to open the URL.
      WebBrowser1.Url = url

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.