[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:
... <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>C:\1.txt</string>
<string>C:\2.txt</string>
<string>C:\3.txt</string>
</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:
MainForm1_Load ...
'Create the empty collection if this is the first run.
If My.Settings.RecentDocuments Is Nothing Then
My.Settings.RecentDocuments = New Specialized.StringCollection
End If
'Load the existing files.
For Each filePath As String In My.Settings.RecentDocuments
Me.AddRecentFile(filePath)
Next filePath
End Sub
MainForm1_FormClosing ...
With My.Settings.RecentDocuments
.Clear()
'Save the current recent file list to the config file.
'I NEED THIS IN REVERSE ORDER?
For Each item As ToolStripMenuItem In Me.RecentDocumentsToolStripMenuItem.DropDownItems
.Add(item.Text)
Next item
End With
End Sub
Private Sub AddRecentFile(ByVal path As String)
Dim items As ToolStripItemCollection = Me.RecentDocumentsToolStripMenuItem.DropDownItems
'Add the new item to the top of the list.
items.Insert(0, New ToolStripMenuItem(path, Nothing, AddressOf SelectRecentFile))
If items.Count > 3 Then
'Remove the last item
items.RemoveAt(items.Count - 1)
End If
End Sub
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
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:
For x As Integer = My.Settings.RecentDocuments.count - 1 To 0 Step -1
Me.AddRecentFile(My.Settings.RecentDocuments(x))
Next
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.