none
How to add recent file history(such as last 3 files accessed) to the Main Menu?

    Question

  • Hello,

    I searched the forums and could not find any direction on how to add a file history to my Main Menu.

    Here's the scenario:

    I open my windows application and select a txt file to open called "File 1".  Later I open "File 2" and "File 3".  I have now closed the windows application.  A day later I open my windows application again. 

    How do I view the recent files selected to my FILE menu?  For example, I would expect to see "File 1", "File 2", and "File 3" under recent files.  This would allow the user to easily reference the past recent files that were opened.  Word and Excel have the feature and I am sure it can be done in VS2005.  Thanks in advance.

    Friday, March 11, 2011 3:47 AM

Answers

  • Thanks Cor Ligthert and bdbodger for your replies.  Your ideas helped me create the solution.  Below is the code I created to strip the full file name ("C:\Desktop\File1.txt") down to a shortened version to be displayed on the Main Menu drop down ("...\File1.txt"). 

    1.  Save the last 4 file names to a system txt file (stored with application files).

    2.  When loading the application, retrieve the 4 file names (full length).

    3.  Find the last "\" like in ("C:\Desktop\File1.txt") and convert the full length file name to a shortened version  ("...\File1.txt") which will displayed to the user on the main menu. 

    4.  If the user selects any one of the 4 recent documents, we load the file contents using the full length name ("C:\Desktop\File1.txt")

    Thanks again!

     Dim Pos, LastPos As Integer, X As Integer = 1
        Dim SearchChar As String = "\"
        'find the last "\" in the filename
        Dim fileLength As Integer
        Dim J As Integer, ShortName As Integer = 1
        For J = 1 To 4
          X = 1 'set starting position each time
          fileLength = Len(RecentFiles(J, 0)) 'determine File Length
          If RecentFiles(J, 0) = Nothing Then Exit For
          Do While X < fileLength
            Pos = InStr(X, RecentFiles(J, 0), SearchChar, CompareMethod.Text)
            X += 1
            If Pos <> 0 Then LastPos = Pos
          Loop
          'set the short file name "RecentFile1.txt" using the last position found for "\"
          RecentFiles(J, ShortName) = "..." + Mid(RecentFiles(J, 0), LastPos)
        Next
    
    
    Tuesday, March 15, 2011 3:32 AM

All replies

  • Use settings . Create a setting of the type System.Collections.Specialized.StringCollection . Save your filenames to that collection and read it at startup . Maybe something like this

      Dim RecentFiles As System.Collections.Specialized.StringCollection
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.RecFiles IsNot Nothing Then
          RecentFiles = My.Settings.RecFiles
          'Code here to use filenames ie RecentFiles(0) to RecentFiles(2) 
        End If
      End Sub


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261

    Friday, March 11, 2011 5:45 AM
  • You can also just create a string setting for each of the number of filenames you want to save . To save 3 filenames create 3 settings .
    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Friday, March 11, 2011 6:01 AM
  • High,

    In addition to the other answers

    A fine class for this kind of operations is the Stack Class

    Module Module1
      Sub Main()
        Dim myStack As New Stack()
        myStack.Push("Hello")
        myStack.Push("World")
        myStack.Push("!")
        PrintValues(myStack)
        Console.ReadLine()
      End Sub
      Public Sub PrintValues(TheStack As Stack)
        For i = 0 To 2
          Console.WriteLine(TheStack(i))
        Next
      End Sub
    End Module
    
    

    To get it in the settings and return it you can serialize and deserialize it.

    http://www.vb-tips.com/SerializeArrayList.aspx


    Success
    Cor
    Friday, March 11, 2011 8:32 AM
  • Thanks Cor Ligthert and bdbodger for your replies.  Your ideas helped me create the solution.  Below is the code I created to strip the full file name ("C:\Desktop\File1.txt") down to a shortened version to be displayed on the Main Menu drop down ("...\File1.txt"). 

    1.  Save the last 4 file names to a system txt file (stored with application files).

    2.  When loading the application, retrieve the 4 file names (full length).

    3.  Find the last "\" like in ("C:\Desktop\File1.txt") and convert the full length file name to a shortened version  ("...\File1.txt") which will displayed to the user on the main menu. 

    4.  If the user selects any one of the 4 recent documents, we load the file contents using the full length name ("C:\Desktop\File1.txt")

    Thanks again!

     Dim Pos, LastPos As Integer, X As Integer = 1
        Dim SearchChar As String = "\"
        'find the last "\" in the filename
        Dim fileLength As Integer
        Dim J As Integer, ShortName As Integer = 1
        For J = 1 To 4
          X = 1 'set starting position each time
          fileLength = Len(RecentFiles(J, 0)) 'determine File Length
          If RecentFiles(J, 0) = Nothing Then Exit For
          Do While X < fileLength
            Pos = InStr(X, RecentFiles(J, 0), SearchChar, CompareMethod.Text)
            X += 1
            If Pos <> 0 Then LastPos = Pos
          Loop
          'set the short file name "RecentFile1.txt" using the last position found for "\"
          RecentFiles(J, ShortName) = "..." + Mid(RecentFiles(J, 0), LastPos)
        Next
    
    
    Tuesday, March 15, 2011 3:32 AM
  • If you import System.IO then you can make a shortened name with two lines of code

        Dim SomeFileName As String = "C:\Desktop\File1.txt"
        Dim Shortname As String = "...\" & Path.GetFileName(SomeFileName)
    

    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Wednesday, March 16, 2011 8:09 AM