В моем приложении есть свойство, которое принимает путь к файлу значка, другое свойство, которое может принимать файл или папку, и другое свойство, которое принимает путь к папке.
Итак, мне пришлось писать вариантыдля каждого из этих свойств ...
Самое простое, и в случае, если вы удовлетворены внешним видом и ограничениями FolderBrowserDialog
, тогда нужно напрямую указать класс System.Windows.Forms.Design.FolderNameEditor
в EditorAttribute
учебный класс.В противном случае Ooki.Dialogs является хорошей библиотекой с открытым исходным кодом в качестве альтернативы, чтобы получить диалоговое окно в современном стиле.
Второй самый простой из них - редактор для выбора пути к файлу значка:
''' <summary>
''' Provides a user interface for selecting a icon file name.
''' </summary>
''' <seealso cref="FileNameEditor"/>
Friend Class IconFileNameEditor : Inherits FileNameEditor
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="IconFileNameEditor"/> class.
''' </summary>
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region " Private Methods "
''' <summary>
''' Initializes the open file dialog when it is created.
''' </summary>
''' <param name="ofd">
''' The <see cref="OpenFileDialog"/> to use to select a file name.
''' </param>
Protected Overrides Sub InitializeDialog(ByVal dlg As OpenFileDialog)
MyBase.InitializeDialog(dlg)
With dlg
.Multiselect = False
.RestoreDirectory = True
.DereferenceLinks = True
.Filter = "Icon Files (*.ico;*.icl;*.exe;*.dll)|*.ico;*.icl;*.exe;*.dll|Icons|*.ico|Libraries|*.dll|Programs|*.exe"
.FilterIndex = 1
.SupportMultiDottedExtensions = True
End With
End Sub
#End Region
End Class
Для выбора пути к файлу или пути к папке, а также для поиска чего-либо уже сделанного и открытого с целью избежать добавления внешних зависимостей в мой проект, я выбрал специальный класс FileFolderDialog
, предоставленный в эта статья, и мне удалось написать редактор так:
''' <summary>
''' Provides a user interface for selecting a file or folder name.
''' </summary>
''' <seealso cref="UITypeEditor"/>
Public Class FileOrFolderNameEditor : Inherits UITypeEditor
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="FileOrFolderNameEditor"/> class.
''' </summary>
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region " Public Methods"
''' <summary>
''' Gets the editor style used by the <see cref="UITypeEditor.EditValue(IServiceProvider, Object)"/> method.
''' </summary>
''' <param name="context">
''' An <see cref="ITypeDescriptorContext"/> that can be used to gain additional context information.
''' </param>
''' <returns>
''' A <see cref="UITypeEditorEditStyle"/> value that indicates the style of editor used
''' by the <see cref="UITypeEditor.EditValue(IServiceProvider, Object)"/> method.
''' <para></para>
''' If the <see cref="UITypeEditor"/> does not support this method,
''' then <see cref="UITypeEditor.GetEditStyle"/> will return <see cref="UITypeEditorEditStyle.None"/>.
''' </returns>
Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
''' <summary>
''' Edits the specified object's value using the editor style indicated by the <see cref="UITypeEditor.GetEditStyle"/> method.
''' </summary>
''' <param name="context">
''' An <see cref="ITypeDescriptorContext"/> that can be used to gain additional context information.
''' </param>
''' <param name="provider">
''' An <see cref="IServiceProvider"/> that this editor can use to obtain services.
''' </param>
''' <param name="value">
''' The object to edit.
''' </param>
''' <returns>
''' The new value of the object.
''' <para></para>
''' If the value of the object has not changed, this should return the same object it was passed.
''' </returns>
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
Using dlg As New OpenFileOrFolderDialog()
If (dlg.ShowDialog = DialogResult.OK) Then
Return dlg.SelectedPath
End If
End Using
Return MyBase.EditValue(context, provider, value)
End Function
#End Region
End Class
Это было довольно просто.