Hi Lance,
Thanks for posting in the community.
First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you have a property which type is
FileInfo in you object, and you would like to show it in the ProperGrid
control. And you want to show an OpenFile Dialog and show the property of
FileInfo in the properertyGrid. Have I fully understood you? If there is
anything I misunderstood, please feel free to let me know.
In Windows Forms we have a FileName Editor class to show the a File Dialog
and let you set the filename into a string property.
But Since you need set it into a property which type is FileInfo. So you
need define a customed PropertyDescriptor whose SetValue could convert a
string into a FileInfo object. Also Your object (which has this FileInfo
property) need implement the ICustomTypeDescriptor interface to let the
PropertyGrid retrieve the customized PropertyDescriptorCollction. Here is
some code snippet to show you how to do this :
[declare an Property in your class]
Private _info As System.IO.FileInfo
Public Property Info() As System.IO.FileInfo
Get
Return _info
End Get
Set(ByVal Value As System.IO.FileInfo)
_info = Value
End Set
End Property
[ICustomTypeDescriptor ]
Public Function GetConverter() As TypeConverter Implements
ICustomTypeDescriptor.GetConverter
Return Nothing
End Function 'GetConverter
Public Function GetEvents(ByVal attributes() As Attribute) As
EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
Return TypeDescriptor.GetEvents(Me, attributes, True)
End Function 'GetEvents
Function GetEvents() As EventDescriptorCollection Implements
System.ComponentModel.ICustomTypeDescriptor.GetEve nts
Return TypeDescriptor.GetEvents(Me, Nothing, True)
End Function 'System.ComponentModel.ICustomTypeDescriptor.GetEv ents
Public Function GetComponentName() As String Implements
ICustomTypeDescriptor.GetComponentName
Return TypeDescriptor.GetComponentName(Me, True)
End Function 'GetComponentName
Public Function GetPropertyOwner(ByVal pd As PropertyDescriptor) As
Object Implements ICustomTypeDescriptor.GetPropertyOwner
Return Me
End Function 'GetPropertyOwner
Public Function GetAttributes() As AttributeCollection Implements
ICustomTypeDescriptor.GetAttributes
Return TypeDescriptor.GetAttributes(Me, True)
End Function 'GetAttributes
'removing the original PropertyDescriptor then add our
FileInfoPropertyDescriptor implementation
Public Function GetProperties(ByVal attributes() As Attribute) As
PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
Dim props As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(Me, attributes, True)
Dim newProps As New ArrayList
Dim prop As PropertyDescriptor
For Each prop In props
If prop.Name <> "Info" Then
newProps.Add(prop)
End If
Next prop
newProps.Add(New FileInfoDescriptor)
Dim propArray As PropertyDescriptor() =
CType(newProps.ToArray(GetType(PropertyDescriptor) ), PropertyDescriptor())
'return props;
Return New PropertyDescriptorCollection(propArray)
End Function 'GetProperties
Function GetProperties() As PropertyDescriptorCollection Implements
System.ComponentModel.ICustomTypeDescriptor.GetPro perties
Return Me.GetProperties(New Attribute() {})
End Function
'System.ComponentModel.ICustomTypeDescriptor.GetPr operties
Public Function GetEditor(ByVal editorBaseType As Type) As Object
Implements ICustomTypeDescriptor.GetEditor
Return Nothing
End Function 'GetEditor
Public Function GetDefaultProperty() As PropertyDescriptor
Implements ICustomTypeDescriptor.GetDefaultProperty
Return Nothing
End Function 'GetDefaultProperty
Public Function GetDefaultEvent() As EventDescriptor Implements
ICustomTypeDescriptor.GetDefaultEvent
Return Nothing
End Function 'GetDefaultEvent
Public Function GetClassName() As String Implements
ICustomTypeDescriptor.GetClassName
Return TypeDescriptor.GetClassName(Me, True)
End Function 'GetClassName
['FileInfoDescriptor]
Public Class FileInfoDescriptor
Inherits PropertyDescriptor
Public Sub New()
MyBase.New("FileInfo", New Attribute() {New
EditorAttribute(GetType(System.Windows.Forms.Desig n.FileNameEditor),
GetType(UITypeEditor)), New
TypeConverterAttribute(GetType(ExpandableObjectCon verter))})
End Sub 'New
'input string -> FileInfo
Public Overrides Sub SetValue(ByVal component As Object, ByVal
value As Object)
Dim col As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(component, Nothing, True)
Dim prop As PropertyDescriptor = col.Find("Info", True)
If Not (prop Is Nothing) Then
prop.SetValue(component, New
System.IO.FileInfo(CType(value, String))) '
End If
End Sub 'SetValue
Public Overrides Function GetValue(ByVal component As Object)
As Object
Dim col As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(component, Nothing, True)
Dim prop As PropertyDescriptor = col.Find("Info", True)
If Not (prop Is Nothing) Then
Return prop.GetValue(component)
End If
Return Nothing
End Function 'GetValue
Public Overrides Function CanResetValue(ByVal component As
Object) As Boolean
Return False
End Function 'CanResetValue
Public Overrides ReadOnly Property ComponentType() As Type
Get
Return Nothing
End Get
End Property
Public Overrides ReadOnly Property PropertyType() As Type
Get
Return GetType(System.IO.FileInfo)
End Get
End Property
Public Overrides ReadOnly Property IsReadOnly() As Boolean
Get
Return False
End Get
End Property
Public Overrides Sub ResetValue(ByVal component As Object)
Dim col As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(component, Nothing, True)
Dim prop As PropertyDescriptor = col.Find("Info", True)
If Not (prop Is Nothing) Then
prop.SetValue(component, Nothing)
End If
End Sub 'ResetValue
Public Overrides Function ShouldSerializeValue(ByVal component
As Object) As Boolean
Return False
End Function 'ShouldSerializeValue
End Class 'FileInfoDescriptor
Hope this helps.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.