OpenDialog in a property Grid How?

I have a property grid that I have associated to an object.
What I want to do is that a property "Path" should appear in my propertygrid and clicking on the right hand side of the grid
should prompt a OpenDialog where i can choose the path of a file.

How can I Do this?

Thanks

[301 byte] By [vbjunkie] at [2007-12-16]
# 1

Add an Editor attribute to your Path property declaration.

[Editor(typeof(System.Windows.Forms.Design.FileNameEditor),
typeof(System.Drawing.Design.UITypeEditor))]

public string Path
{
get
{
return _path;
}
set
{
_path = value;
}
}
Also, you have to reference System.Design.dll to your project.

Regards,

-chris

gwapo at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2

Thanks a lot that worked!!

One last thing what about filtering etc how would you apply those?
usually i would do something like



'Public Sub LocateFile()
' With mPath
.AddExtension = True
.CheckFileExists = True
.CheckPathExists = True
.Filter = "*.vb|*.vb"
'If TxtDALPath.Text.Trim.Length > 0 Then
' .FileName = TxtDALPath.Text
'End If
'If .ShowDialog() = DialogResult.OK Then
' TxtDALPath.Text = .FileName
'End If
End With

'End Sub


'//your suggested code that worked
'//Need to add a filter + a descriptionAttribute

<Editor(GetType(System.Windows.Forms.Design.FileNameEditor), _
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property path() As String
Get
Return mPath
End Get
Set(ByVal value As String)
mPath = value
End Set
End Property


thanks again
vbjunkie

vbjunkie at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3

In case like that, you'll have to create your own UITypeEditor, in which you can do whatever you want, like showing an OpenFileDialog with filename filtering. I have a simple C# UITypeEditor that do just that, you can pattern your own UITypeEditor with mine:



internal class FilteredFileNameEditor : UITypeEditor
{
private OpenFileDialog ofd = new OpenFileDialog();

public override UITypeEditorEditStyle GetEditStyle(
ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}

public override object EditValue(
ITypeDescriptorContext context,
IServiceProvider provider,
object value)
{
ofd.FileName = value.ToString();
ofd.Filter = "Text File|*.txt|All Files|*.*";
if (ofd.ShowDialog() == DialogResult.OK )
{
return ofd.FileName;
}
return base.EditValue (context, provider, value);
}
}


Then, instead of using the default .NET FileNameEditor, use your own created UITypeEditor.
Regards,

-chris

gwapo at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 4
Thanks again

For providing the code.Sorry for not replying in c#
I have included the code in vb in case somebody needs it.
thanks
vbjunkie


Imports System.ComponentModel
Imports System.Drawing.Design

Friend Class FilteredFileNameEditor
Inherits UITypeEditor

Private ofd As New OpenFileDialog

Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle

Return UITypeEditorEditStyle.Modal

End Function

Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object

Dim ofd As OpenFileDialog = New OpenFileDialog

ofd.FileName = value.ToString()

ofd.Filter = "Text File|*.txt|All Files|*.*"

If ofd.ShowDialog() = DialogResult.OK Then

Return ofd.FileName

End If

Return MyBase.EditValue(context, provider, value)

End Function

End Class



'//The Object to associate to the property grid


Public class Customer

<Editor(GetType(FilteredFileNameEditor), _
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property path() As String
Get
Return mPath
e
nd Get
Set(ByVal value As String)
mPath = value
End Set

End Property

end class




'//In the form Load event

private mCustomer as New Customer
propertygrid1.selectedObject=mCustomer


vbjunkie at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 5

You may want to replace the following:

ofd.FileName = value.ToString()

With:

If value IsNot Nothing Then

ofd.FileName = value.ToString()

End If

AntonioChagoury at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Designer...