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]
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
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
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
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 end Get Set(ByVal value As String) mPath = value End Set End Propertyend class |
| |
'//In the form Load eventprivate mCustomer as New Customer propertygrid1.selectedObject=mCustomer
|
You may want to replace the following:
ofd.FileName = value.ToString()
With:
If value IsNot Nothing Then
ofd.FileName = value.ToString()
End If