Results 1 to 4 of 4

Thread: [RESOLVED] MRU (Recent file list) - How to make it in reverse order?

  1. #1

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Resolved [RESOLVED] MRU (Recent file list) - How to make it in reverse order?

    .Net 3 framework
    VS 2008 - VB.net

    I picked up some code for making a Recent file list in Files Menu.
    Code saves on FormClose paths to My.Settings System.Collections.Specialized.String.Collection, User scope, maxPaths=3

    Saving and MenuClick works ok, but the problem is when I open in this order:
    1.txt
    2.txt
    3.txt
    they show up in menu like:
    3.txt
    2.txt
    1.txt
    till now ok - last accesed file is top on menu
    I close Program (MainForm_FormClosing), reopened it and here is problem (they show in reverse order):
    1.txt
    2.txt
    3.txt
    I understand why is that (they are saved in that order in documents&settings... user.config file:
    VB Code:
    1. ... <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2.                         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    3.                         <string>C:\1.txt</string>
    4.                         <string>C:\2.txt</string>
    5.                         <string>C:\3.txt</string>
    6.                     </ArrayOfString> ...

    So Question: How Can I save ArrayOfString in user.config in reverse order (First save last Item from ToolStripMenuItems then up till 1st)?

    Codes:
    Vb Code:
    1. MainForm1_Load ...
    2.         'Create the empty collection if this is the first run.
    3.         If My.Settings.RecentDocuments Is Nothing Then
    4.             My.Settings.RecentDocuments = New Specialized.StringCollection
    5.         End If
    6.         'Load the existing files.
    7.         For Each filePath As String In My.Settings.RecentDocuments
    8.             Me.AddRecentFile(filePath)
    9.         Next filePath
    10. End Sub
    11.  
    12. MainForm1_FormClosing ...
    13.      With My.Settings.RecentDocuments
    14.         .Clear()
    15.         'Save the current recent file list to the config file.
    16.        
    17.         'I NEED THIS IN REVERSE ORDER?
    18.         For Each item As ToolStripMenuItem In Me.RecentDocumentsToolStripMenuItem.DropDownItems
    19.         .Add(item.Text)
    20.         Next item
    21.      
    22.      End With
    23. End Sub
    24.  
    25. Private Sub AddRecentFile(ByVal path As String)
    26.         Dim items As ToolStripItemCollection = Me.RecentDocumentsToolStripMenuItem.DropDownItems
    27.         'Add the new item to the top of the list.
    28.         items.Insert(0, New ToolStripMenuItem(path, Nothing, AddressOf SelectRecentFile))
    29.         If items.Count > 3 Then
    30.             'Remove the last item
    31.             items.RemoveAt(items.Count - 1)
    32.         End If
    33. End Sub

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: MRU (Recent file list) - How to make it in reverse order?

    If you need to loop in a specific order then don't use For Each loop. Use For loop with a counter instead.

    Having said that, you'll nedd to change your for each loop to a for loop and looping in reverse order, something like this
    Code:
    For i As Integer = Me.RecentDocumentsToolStripMenuItem.DropDownItems.Count - 1 to 0 Step -1
            Me.RecentDocumentsToolStripMenuItem.DropDownItems(i).Add(item.Text)        
    Next i
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: MRU (Recent file list) - How to make it in reverse order?

    just load them in reverse order in your mainform_load procedure

    vb Code:
    1. For x As Integer = My.Settings.RecentDocuments.count - 1 To 0 Step -1
    2.      Me.AddRecentFile(My.Settings.RecentDocuments(x))
    3. Next

  4. #4

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Resolved Re: MRU (Recent file list) - How to make it in reverse order?

    Thanks guys.
    I was so close but so far away from solution
    It's working now.

Posting Permissions

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



Click Here to Expand Forum to Full Width