VB2012: このサイトhttp://www.codeproject.com/Articles/88564/MRU-Menu-Class-Revisited#xx4295995xxからコードを取得して、プロジェクトに再利用可能な MRU クラスを実装しました。My.Settings 名前空間を使用するように変更した特定の XML ファイルに書き込むのではなく、すべてがうまく機能します。アイテムの読み込みは正常に機能しますが、保存は固執していません。これは、プロパティが ByVal として設定されているためだと思います。
load イベントでは、文字列コレクション mruList.FileList = My.Settings.MruInputFiles を割り当てるので、これは参照割り当てになると思いました。ただし、文字列コレクションを保存すると、固執していないようです。My.Settings を手動で追加してテストしましたが、それは機能しますが、クラス プロパティを介して割り当てる方法があるはずです。クラスプロパティの設定だけでこれを機能させるために何をすべきかがわかりません。
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