CodingCool VB.NET Simple WebBrowser with VB.NET

Simple WebBrowser with VB.NET

Today we are going to create a simple web browser with VB.NET. Let’s get started.

UI

The UI for this project requires the following components.

  • a toolstrip
  • 9 toolstrip buttons
  • a tool strip textbox
  • a listbox
  • a WebBrowser control

Apply these settings to the Form:

NamefrmMain
Size1162, 614
TextSimple WebBrowser
Form

Then rename the file to frmMain.

Apply these settings to the buttons:

NameText
btnSettingsSettings
btnHomeHome
btnBackBack
btnForwardForward
btnRefreshRefresh
btnGoGo
btnAddAdd
btnDeleteDelete
btnClearClear
Buttons

Apply these settings to the TextBox:

NametxtURL
Width655
TextBox

Apply these settings to the ListBox:

NamelstBookMarks
DockRight
Width168
ListBox

Apply these settings to the WeBrowser:

NamewbMain
Size960, 547
AnchorTop, Bottom, Left, Right
WebBrowser

When you’re done it should resemble this:

Then add this code:

Imports System.IO
Public Class frmMain
    Private dtweb As DataTable
    Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btnHome.Click
        txtURL.Text = My.Computer.FileSystem.ReadAllText("your path")
        wbMain.Navigate(txtURL.Text)
    End Sub
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dtweb = New DataTable()
        dtweb.Columns.Add("web", GetType(String))
        Using r As StreamReader = New StreamReader("your path)
            Dim line As String
            line = r.ReadLine
            If Not IsNothing(line) Then
                If line.Length > 0 Then
                    Dim dr As DataRow = dtweb.NewRow
                    dr.Item("web") = line
                    dtweb.Rows.Add(dr)
                End If
            End If
            Do While (Not line Is Nothing)
                Console.WriteLine(line)
                line = r.ReadLine
                If Not IsNothing(line) Then
                    If line.Length > 0 Then
                        Dim dr As DataRow = dtweb.NewRow
                        dr.Item("web") = line
                        dtweb.Rows.Add(dr)
                    End If
                End If
            Loop
        End Using
        lstBookMarks.DataSource = dtweb
        lstBookMarks.DisplayMember = "web"
        txtURL.Text = My.Computer.FileSystem.ReadAllText("your path")
        wbMain.Navigate(txtURL.Text)
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim strNewBookMark As String = txtURL.Text
        For Each dr As DataRow In dtweb.Rows
            If dr(0) = strNewBookMark Then
                MessageBox.Show("This bookmark already exists and will Not be added")
                Exit Sub
            End If
        Next
        dtweb.Rows.Add(txtURL.Text)
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("your path", False)
        For Each dr As DataRow In dtweb.Rows
            file.WriteLine(dr(0))
        Next
        file.Close()
    End Sub
    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        wbMain.GoBack()
    End Sub
    Private Sub btnForward_Click(sender As Object, e As EventArgs) Handles btnForward.Click
        wbMain.GoForward()
    End Sub
    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        wbMain.Refresh()
    End Sub
    Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
        wbMain.Navigate(txtURL.Text)
    End Sub
    Private Sub lstBookMarks_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstBookMarks.SelectedValueChanged
        If Not IsNothing(sender.selecteditem) Then
            wbMain.Navigate(sender.selecteditem.item(0))
            txtURL.Text = sender.selecteditem.item(0)
        End If
    End Sub
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        dtweb.Clear()
    End Sub
    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        dtweb.Rows.RemoveAt(lstBookMarks.SelectedIndex)
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("your path", False)
        For Each dr As DataRow In dtweb.Rows
            file.WriteLine(dr(0))
        Next
        file.Close()
    End Sub
End Class

Add a new form called frmSettings. Then add the following components:

  • a label
  • a button
  • a textbox
  • a listbox

Apply the following settings to the form:

TextSettings
Size452, 489
Form

Apply the following settings to the components:

ComponentNameSizeLocationText
LabelLabel1N/A12, 9Home Page:
ButtonbtnSave75, 23348, 4Save
TextBoxtxtHomePage261, 2081, 6N/A
ListBoxlstPages408, 40715, 32N/A
Components

Next, add this code:

Public Class frmSettings
    Private dtweb As DataTable
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("your path", False)
        file.WriteLine(txtHomePage.Text)
        file.Close()
    End Sub
    Private Sub frmSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtHomePage.Text = My.Computer.FileSystem.ReadAllText("your path")
        dtweb = New DataTable()
        dtweb.Columns.Add("web", GetType(String))
        dtweb.Rows.Add("bing.com")
        dtweb.Rows.Add("google.com")
        dtweb.Rows.Add("ecosia.com")
        dtweb.Rows.Add("duckduckgo.com")
        lstPages.DataSource = dtweb
        lstPages.DisplayMember = "web"
        lstPages.ValueMember = "web"
    End Sub
    Private Sub lstPages_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstPages.SelectedValueChanged
        Try
            txtHomePage.Text = lstPages.SelectedValue
        Catch ex As Exception
        End Try
    End Sub
End Class

Add this code to the bottom of the main form:

    Private Sub btnSettings_Click(sender As Object, e As EventArgs) Handles btnSettings.Click
        frmSettings.ShowDialog()
    End Sub

Next, go to the paths you specified and create those files.
Finally hit the start button and enjoy.

Please note that the browser component relies on the Trident rendering engine which is now deprecated.

Leave a Reply

Your email address will not be published. Required fields are marked *