MRU with StringCollection

303 views Asked by At

VB2012: I've taken the code from this site http://www.codeproject.com/Articles/88564/MRU-Menu-Class-Revisited#xx4295995xx to implement an MRU re-usable class for my project. All works great except rather than to write to a specific XML file I modified it to just use the My.Setttings namespace. The loading of items works fine but the saving just isnt sticking and i think its because of the properties are set as ByVal.

In the load event I assign the string collection mruList.FileList = My.Settings.MruInputFiles so I thought this would be a reference assignment. However when I go and save the string collection it doesn't seem to stick. I tested by adding the My.Settings manually and that works but there should be a way to assign it through the class properties no? I'm just not getting what I should be doing to make this work with just the setting of the class properties.

Public Class frmMain
    Public WithEvents mruList As CMRmedia.MRUMenu
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            mruList = New CMRmedia.MRUMenu(mnuLoadRecentInputs, Me)

            'mruList.FileName = "x:\Path\To\My\File.xml"
            mruList.FileList = My.Settings.MruInputFiles
            mruList.MaxItems = My.Settings.MruMaxInputFiles
            mruList.ShowClearRecent = True
            mruList.Validate = True
            mruList.StoreRelativePaths = False

            'Loads the MRU list for persisted file if it exists
            mruList.Load()
    End Sub

    Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'save up the user settings
        With My.Settings
            'save the MRU list to persisted file before closing the application
            mruList.Save()
           .MruInputFiles = mruList.FileList 'this makes sure the file list gets assigned back to the settings

            .Save()
        End With
    End Sub

End Class

Namespace CMRmedia
    ''' <summary>
    ''' Menu Class for automating handling of most recently used file menus
    ''' </summary>
    Public Class MRUMenu
        Private _MruList As New System.Collections.Generic.List(Of MruItem)
        Private WithEvents _menuItem As ToolStripMenuItem
        Private _mru As ToolStripMenuItem
        'Private _FileName As String
        Private _FileList As StringCollection
        Private _MaxItems As Integer

        ''' <summary>
        ''' Get or Set the string collection for storing the MRU list
        ''' </summary>
        Public Property FileList() As StringCollection
            Get
                Return _FileList
            End Get
            Set(ByVal value As StringCollection)
                _FileList = value
            End Set
        End Property

    ''' <summary>
    ''' Saves the current MRU list to the MRU string collection
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Save()
        Try
            'if the string collection has not been created then we create it here
            If FileList Is Nothing Then FileList = New StringCollection

            'clear the current contents of the string collection
            FileList.Clear()

            'now we add the contents of the menus to the string collection
            Dim mnu As MruItem
            For Each mnu In _MruList
                Dim _path As String = mnu.FilePath
                If _storeRelativePaths Then _path = transformPath(_path)
                FileList.Add(_path)
            Next mnu

            'test to see if we manually assign to the My.Settings then it works.
            'My.Settings.MruInputFiles = FileList
            'My.Settings.MruMaxInputFiles = MaxItems
        Catch ex As Exception
            Dim e As New MruException
            e.ErrorType = MruException.MruErrorType.FileListWriteError
            e.innerException = ex.Message
            e.message = "Error writing the MRU Settings"
            RaiseEvent MruError(e)
        End Try
    End Sub

    End Class

End Namespace
0

There are 0 answers