Dienstag, 28. Mai 2013

VB DOT Net: Making a Recent File list

There are several ways to create a recent file list. I did not found an official way, so i made my own:

1) I will save recent files in a separate file RecentFiles_Filename inside user app data. During software i will use the files inside a string array named as RecentFiles_List. I will work with 5 recent files.

    Public Const RecentFiles_MaxConst = 5
    Private RecentFiles_List(RecentFiles_MaxConst) As String
    Private RecentFiles_Filename As String = Application.UserAppDataPath & "\Recent_Files_List.txt"


2) During startup inside Private Sub Form_Open_Shown

        'Read recent files from file. save it to array
        Try
            Me.RecentFiles_List = IO.File.ReadAllLines(Me.RecentFiles_Filename)
        Catch ex As Exception
            If Form1.DebugMode Then
                MessageBox.Show(ex.Message, "Read Recent File List:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            End If
        End Try

        'It does not matter how much inside file, we cut the list at 5 entrys!
        If Me.RecentFiles_List.Length > Form_Open.RecentFiles_MaxConst Then
            Array.Resize(Me.RecentFiles_List, Form_Open.RecentFiles_MaxConst)
        End If

        'Recent Files. Show Array
        Me.Show_RecentFiles()


3) During closing Form_Open_FormClosing

        'Close: Save Recent File Array to list
        Try
            IO.File.WriteAllLines(Me.RecentFiles_Filename, Me.RecentFiles_List)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Write Recent File List:", MessageBoxButtons.OK)
        End Try

4) During opening a file i need this:

        Me.Update_RecentFiles(myFile)


5) And this are the main subs:




    Private Sub Show_RecentFiles()
        '....................................................
        'Show Recent Files. from array into listbox
        '....................................................

        Me.ListBox1.Items.Clear()

        For i As Integer = 0 To RecentFiles_MaxConst - 1
            If String.IsNullOrEmpty(Me.RecentFiles_List(i)) = False Then
                Me.ListBox1.Items.Add(Me.RecentFiles_List(i))
            End If
        Next
    End Sub

    Private Sub Update_RecentFiles(ByVal myFile As String)
        '....................................................
        'Update Recent File List. Parameter1: Dateiname akt Datei
        '....................................................

        'Check ob File bereits in Recent List. Falls JA: Exit!
        For i As Integer = 0 To RecentFiles_MaxConst - 1
            If myFile = RecentFiles_List(i) Then
                Exit Sub
            End If
        Next


        'Recent List um 1 verschieben. Neue Datei einfügen
        RecentFiles_List(4) = RecentFiles_List(3)
        RecentFiles_List(3) = RecentFiles_List(2)
        RecentFiles_List(2) = RecentFiles_List(1)
        RecentFiles_List(1) = RecentFiles_List(0)

        RecentFiles_List(0) = myFile

    End Sub

We use this inside our stock and management software:
http://www.terminal-systems.de/wws-lite-win.htm