473,241 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Exposing System.IO.FileInfo in a PropertyGrid

I want to expose properties of a class to a user via a PropertyGrid class. Some of the properties are of type System.IO.FileInfo. Ideally, an OpenFileDialog window would appear when the user attempted to edit the value of the System.IO.FileInfo properties. Is there an existing UITypeEditor that will do this type of thing, or will I need to create my own

Thanks
Lance
Nov 20 '05 #1
5 6504
Check out the EditorBrowsableAttribute Class

Regards - OHM

Lance wrote:
I want to expose properties of a class to a user via a PropertyGrid
class. Some of the properties are of type System.IO.FileInfo.
Ideally, an OpenFileDialog window would appear when the user
attempted to edit the value of the System.IO.FileInfo properties. Is
there an existing UITypeEditor that will do this type of thing, or
will I need to create my own?

Thanks,
Lance


--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com
Nov 20 '05 #2
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.

Nov 20 '05 #3
Wow. That looks like what I needed. Thanks a lot for the example. I'll give it a try

Lance
Nov 20 '05 #4
Not quite what I was looking for, but interesting nonetheless. Thanks

----- One Handed Man wrote: ----

Check out the EditorBrowsableAttribute Clas

Regards - OH

Nov 20 '05 #5
Peter,
This question is not related to theOP or even VB.NET, but I hope
you may know someone who you can ask a question for me on ISmartDocument in
the SmartTags 2.0 library.

I am trying to get J# to work with this interface but I am having a terrible
time trying to do so. Do you know or could you find out if this interface is
sanctioned to work with J# or not. It may save me some considerable time.

Regards - OHM

Peter Huang wrote:
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.


--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Terry | last post by:
I have a very basic program, but for some reason I can't get it to behave properly. What I want is a basic form with a TabControl that fills the entire form. The tab control should have 4 tabs...
1
by: S.mark | last post by:
Hi, I can't find the ReadOnly property in PropertyGrid control (Visual Studio 2005), is there any work around to display object properties with the new PropertyGrid control in ReadOnly mode ? ...
1
by: ANDRES BECERRA | last post by:
Herfried K. Wagner was kind enough to point me to the PropertyGrid control http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformspropertygridclasstopic.asp I have found a few...
7
by: siddhiash | last post by:
Hi Friends I want to add PasswordChar Property which shows ****** for string which I type in PropertyGrid Control. Regards, Siddharth
1
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I...
9
by: Arpan | last post by:
If I am not wrong, System.IO is one of the seven namespaces that get imported in all ASP.NET page automatically but in the following code: <%@ Import Namespace="System.IO" %> <script...
4
by: phcmi | last post by:
I have a PropertyGrid question. My task is to replace a legacy dialog box presentation with a modern one. The dialog itself allows the user to set configuration settings in our application, so...
3
by: =?Utf-8?B?U3RldmVU?= | last post by:
Is it possible to hide a row within a PropertyGrid based upon the boolean value of another row within the PropertyGrid? I am using VS2005 with .NET Frameworks 2.0. -- ----------- Thanks,...
1
by: nygiantswin2005 | last post by:
Hi I am trying to resolve this bug that I have in this application. The code is below. It will generate this Exception System.UnauthorizedAccessException: Access to the path is denied. at...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.