473,378 Members | 1,609 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,378 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 6516
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.