VB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox-VBForums
Results 1 to 2 of 2

Thread: VB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox

  1. #1

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,019

    Lightbulb VB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox

    Disclaimer: Code Comes from my browser project which is licensed under the Mozilla Public License Version 2

    Hello!

    This time I would like to share with you a easy method of Saving History using a textfile. Now please note it is best to use a folder such as AppData to store it in when compiling.

    This example can be used on either A) A Text Box or B) A Rich Text Control. and used with Webbrowser control or one of the first ones mentioned.

    What It Does

    Saves History of Webbrowser control (ONLY what is navigated in your app) - Rich Text Box - Textbox.

    Then,

    when you load the History View form, all the changes/history is loaded from a Saved Text File via Setting

    Getting Startedd


    What you need:

    A Text File
    A Setting named History

    (2) Windows Forms

    Form 1 : Main Form
    A ToolStrip
    A Text Box
    (4) Buttons : Navigate, GoBack, GoForward,And Refresh.
    (1) Button to open the History View form.
    Webbrowser Control.

    Form2 : History View

    ListBox
    Context Menu
    Add (2) items : Delete, Delete All


    Code

    Main Form

    Note: Import System.IO
    First of all you need to add this public sub to handle the "Adding History" Part.

    Code:
      Private Sub Done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs)
            Tabcontrol1.SelectedTab.Text = CType(Tabcontrol1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
            ToolStripTextBox1.Text = CType(Tabcontrol1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString
            History.ListBox1.Items.Add(ToolStripTextBox1.Text)
            Save_History()
    End Sub
    Next, You need to add the Save_History Sub:

    Code:
     Public Sub Save_History()
    Dim HistoryPath as String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & 
                             "\ApplicationName" & 
                             My.Settings.History
            File.WriteAllLines(FavPath, ListBox1.Items.Cast(Of String)())
            Dim file As System.IO.StreamWriter
            file = My.Computer.FileSystem.OpenTextFileWriter(HistoryPath, True)
            For Each Str As String In HistoryView.ListBox1.Items
                file.WriteLine(Str)
            Next
            file.Close()
        End Sub
    Now - One last step.

    When you navigate - You need to add a Handler.

    Code:
    AddHandler brws.DocumentCompleted, AddressOf Done
    This needs to be added to each of the Navigate, Go Forward, GoBack, and Refresh.


    Do Not Forget To Add The Code To Open History View!!!

    History View

    Note: Import System.IO

    Now to work on History View!

    In order for it to all come together be sure to have completed ALL Steps in the What You Need Section!
    This code should be added for the Form_Load - It loads all the saved History into the TextBox.

    You need this additonal Piece of code to make it work:
    Code:
    Dim HistoryList as String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & 
                             "\Application" & 
                             My.Settings.History

    Code:
            ListBox1.Items.AddRange(File.ReadAllLines(HistoryList))

    Delete Context Menu Button
    Code:
      ListBox1.Items.Remove(ListBox1.SelectedItem)
            File.WriteAllLines(HistoryList, ListBox1.Items.Cast(Of String)())
    Delete All (Clear ALL)

    Code:
      ListBox1.Items.Clear()
            File.WriteAllLines(HistoryList, ListBox1.Items.Cast(Of String)())

    Using in RichTextBox Setting (such as Notepad) OR TextBox

    The following can be used in a RichTextBox also - using the same things as below - However - We do not need the
    "Done" Sub You just simply add the Text to the History Form, instead.

    Like so:

    Code:
      HistoryView.ListBox1.Items.Add(RichTextbox1.Text)
            Save_History()
    The rest such as the loading of the items in HistoryView as well as Deleting items, is the same as in the Browser Example.

    Conclusion

    This is a easy to use Example on recording changes in your Windows Form Application Enjoy!

    If you have suggestions, Ideas, Code Clean up just let me know!



    Update

    The Saving changes of a richtextbox seems to have stopped working. Not sure why. If you get it to work good for you otherwise, this will only record browser history - The only time I experience issues with the richtextbox is if you paste a lot of words at once. I suppose since the entry is so large it can't handle it.

    Additonally, you only need to specify a path like so in your setting:

    Code:
    /{AppName}DataFolder/FileName.txt
    Be sure to Add a folder name it, and place any files that will be used like presented - in that folder.
    Last edited by jdc20181; Feb 10th, 2017 at 04:17 PM. Reason: Added Fixes to resolve errors.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  2. #2

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,019

    Re: VB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbo

    Update:

    I guess since the file isn't processed - and stored automatically you must manually do it. So - I am updating the code to resolve this issue.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.