Understanding my Web Browser Code

sutcolstudent

New member
Joined
Mar 30, 2009
Messages
2
Programming Experience
Beginner
Hi guys. Could someone explain and/ or comment on what each part of my code does. I know there is alot, but I would appreciate it. I have been asked to write a report for my assignment and I am really stuck !

Thank you

Paul

VB.NET:
Imports System.IO
Public Class form1
    Private wbBrowserTab(0) As WebBrowser

    Private currentTab As Integer
    Dim linetext As String
    Dim history1(0) As histobj
    Public hiturl(0) As String
    Public hitcount(0) As Integer

    Private Const BLOCK_DEFAULT As String = "[DEFAULT]"
    Private Const BLOCK_INTERNETSHORTCUT As String = "[InternetShortcut]"
    Private Const ITEM_URL As String = "URL="
    Private Const ITEM_BASEURL As String = "BASEURL="


    Private Sub Navigate(ByRef URL As String)
        If String.IsNullOrEmpty(URL) Then Return
        If URL.Equals("about:blank") Then Return
        If Not URL.StartsWith("http://") And _
        Not URL.StartsWith("https://") Then
            URL = "http://" & URL
        End If
        Try
            wbBrowserTab(currentTab).Navigate(URL)
        Catch ex As Exception
            ' Do nothing on Exception
        End Try

        If Not history1(history1.Length - 1) Is Nothing Then
            ReDim Preserve history1(history1.Length)
        End If
        history1(history1.Length - 1) = New histobj(URL, Date.Now)

        hitcounter(history1(history1.Length - 1).gethostURL)


    End Sub
    Private Sub hitcounter(ByVal url As String)
        Dim n As Integer
        For n = 0 To hitURL.Length - 1
            If url = hitURL(n) Then
                hitcount(n) += 1
                Exit For
            End If
        Next
        If n = hiturl.Length Then
            For n = 0 To hiturl.Length - 1
                If hiturl(n) = "" Then
                    Exit For
                End If
            Next
            If n = hiturl.Length Then
                ReDim Preserve hiturl(hiturl.Length + 9)
                ReDim Preserve hitcount(hitcount.Length + 9)

            End If
            hiturl(n) = url
            hitcount(n) += 1
        End If
    End Sub
    Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim history As String = "./history.txt"
        Dim textout As IO.StreamWriter = File.AppendText(history)
        Dim b As Integer
        Dim linetext As String
        For b = 0 To history1.Length - 1
            If history1(b) Is Nothing Then
                'ignore if nothing'
            Else
                linetext = history1(b).geturlvisited & "," & history1(b).gettimestamp.ToString
                textout.WriteLine(linetext)
            End If


        Next
        textout.Flush()
        textout.Close()



    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        wbBrowserTab(0) = New WebBrowser
        TabControl1.TabPages(0).Controls.Add(wbBrowserTab(0))
        wbBrowserTab(0).Dock = DockStyle.Fill

        Me.Text = "Web Browser"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TabControl1.TabPages.Add("New tab page")


        TabControl1.SelectedIndex() = TabControl1.TabCount - 1
        currentTab = TabControl1.SelectedIndex

        While currentTab >= wbBrowserTab.Length
            ReDim Preserve wbBrowserTab(wbBrowserTab.Length)
        End While


        wbBrowserTab(currentTab) = New WebBrowser
        wbBrowserTab(currentTab).Dock = DockStyle.Fill

        TabControl1.TabPages(currentTab).Controls.Add(WebBrowser1)

    End Sub

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        WebBrowser1.Navigate(txtURL.Text)
    End Sub

    Private Sub btnforward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnforward.Click
        On Error Resume Next
        WebBrowser1.GoForward()
    End Sub

    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        On Error Resume Next
        WebBrowser1.GoBack()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        WebBrowser1.Refresh()
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        WebBrowser1.Stop()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim Open As New OpenFileDialog()
        Dim URLFile As String()
        Dim myStreamReader As System.IO.StreamReader
        Open.Filter = "Internet Favourite (*.url)|*.url|All files (*.*)|*.*"
        Open.CheckFileExists = True
        Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            URLFile = myStreamReader.ReadToEnd().Split(New String() {ControlChars.CrLf}, _
                                                     StringSplitOptions.RemoveEmptyEntries)
            For Each Item As String In URLFile
                If Item.StartsWith(ITEM_URL) Then
                    Navigate(Item.Substring(ITEM_URL.Length))
                    Exit For
                End If
            Next
        Catch ex As Exception
            ' Do nothing on Exception
        End Try

    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter
        Save.Filter = "Internet Favourite (*.url)|*.url|All files (*.*)|*.*"
        Save.CheckPathExists = True
        Save.FileName = wbBrowserTab(currentTab).DocumentTitle
        Save.ShowDialog(Me)
        Try
            myStreamWriter = System.IO.File.CreateText(Save.FileName)
            myStreamWriter.Write(BLOCK_DEFAULT & vbCrLf & _
                                 ITEM_BASEURL & wbBrowserTab(currentTab).Url.ToString & vbCrLf & _
                                 BLOCK_INTERNETSHORTCUT & vbCrLf & _
                                 ITEM_URL & wbBrowserTab(currentTab).Url.ToString)
            myStreamWriter.Flush()
        Catch ex As Exception
            ' Do nothing on Exception
        End Try

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Dim Response As MsgBoxResult
        Response = MsgBox("Are you sure you want to Exit Web Browser?", _
                          MsgBoxStyle.Question + MsgBoxStyle.YesNo, _
                          "Web Browser")
        If Response = MsgBoxResult.Yes Then
            End
        End If

    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        txtURL.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        txtURL.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        txtURL.Paste()
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
        txtURL.SelectAll()
    End Sub

    Private Sub GoToToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToToolStripMenuItem.Click
        Navigate(txtURL.Text)
    End Sub

    Private Sub HomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HomeToolStripMenuItem.Click
        WebBrowser1.GoHome()
    End Sub

    Private Sub txtURL_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtURL.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            Navigate(txtURL.Text)
        End If
    End Sub


    Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        Me.Text = "Web Browser - " & WebBrowser1.DocumentTitle
        txtURL.Text = WebBrowser1.Url.ToString

        status.Text = e.Url.ToString()
        Me.Text = WebBrowser1.DocumentTitle & " - Web Browser"
        ProgressBar1.Visible = True

    End Sub

    Private Sub WebBrowser1_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
        ProgressBar1.Value = CInt(e.CurrentProgress)
    End Sub

    Private Sub HistoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HistoryToolStripMenuItem.Click
        history_2.Visible = True

    End Sub

    Private Sub HitCounterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HitCounterToolStripMenuItem.Click
        Hit_Counter.Visible = True
    End Sub
End Class
 
Last edited by a moderator:
Hi guys. Could someone explain and/ or comment on what each part of my code does.
Isn't that like asking us to tell you why you combed your hair like that this morning?

If I wrote code, but couldn't remember why, I'd be a little concerned come exam time.. Not being able to remember things in order to answer questions would sure make things difficult
 
lol , there is a long source code but easy to understand , i think u could search each part of it in the internet & get ur answer more faster than waiting days..
btw there is a webbrowser that have some sub in ur app.
one sub check if url doesn't contain any http or https , it will add it to the first of url & the navigate it , also check history visited sites & reports at the end which urls has been visited.
it can open some files w some extentions like url & ...
also can save them w page title name
open file dialog & save file dialog also needed ...
& some other little stuffs
 
Back
Top