Windows Develop Bookmark and Share   
 index > Windows Forms General > Property Grid with file path
 

Property Grid with file path

I want to select a file in a propertygrid and use its file name and path seperatly in my class.

What can I do

ZadehDyn  Saturday, April 07, 2007 7:59 AM

Youmust apply an System.ComponentModel.Editor attribute on your property. Also, you must add a reference to the following assemblies: System.dll, System.Drawing.dll and System.Design.dll.

C#

private string _fileName;

[System.ComponentModel.Editor(

typeof(System.Windows.Forms.Design.FileNameEditor),

typeof(System.Drawing.Design.UITypeEditor))]

public string FileName

{

get { return this._fileName; }

set { this._fileName = value; }

}

Visual Basic

Dim _fileName As String

<System.ComponentModel.Editor( _

GetType(System.Windows.Forms.Design.FileNameEditor), _

GetType(System.Drawing.Design.UITypeEditor))> _

Public Property FileName() As String

Get

Return Me._fileName

End Get

Set(ByVal value As String)

Me._fileName = value

End Set

End Property

Cristian_t  Saturday, April 07, 2007 9:03 AM

you can Add 2 read-only properties with PATH and Filename.

When you select the FULL Path you'll set the right values of this properties in this way everything will works.

Dario

Dario Galvani  Saturday, April 07, 2007 8:37 AM

Youmust apply an System.ComponentModel.Editor attribute on your property. Also, you must add a reference to the following assemblies: System.dll, System.Drawing.dll and System.Design.dll.

C#

private string _fileName;

[System.ComponentModel.Editor(

typeof(System.Windows.Forms.Design.FileNameEditor),

typeof(System.Drawing.Design.UITypeEditor))]

public string FileName

{

get { return this._fileName; }

set { this._fileName = value; }

}

Visual Basic

Dim _fileName As String

<System.ComponentModel.Editor( _

GetType(System.Windows.Forms.Design.FileNameEditor), _

GetType(System.Drawing.Design.UITypeEditor))> _

Public Property FileName() As String

Get

Return Me._fileName

End Get

Set(ByVal value As String)

Me._fileName = value

End Set

End Property

Cristian_t  Saturday, April 07, 2007 9:03 AM
Thank for help it is very useful,I have one last problem for this. How I can filter at this point. I want to filter for .dxf files.
ZadehDyn  Saturday, April 07, 2007 9:33 AM

Toachieve thisyou mightcreate your own editor by inheriting from UITypeEditor.

C#

public class CustomFileNameEditor : UITypeEditor

{

public CustomFileNameEditor()

{

}

private string _customFilter = "My files(*.dxf)|*.dxf";

[DefaultValue("My files(*.dxf)|*.dxf")]

public string CustomFilter

{

get { return _customFilter; }

set { _customFilter = value; }

}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)

{

// We'll show modal dialog (OpenFileDialog)

return UITypeEditorEditStyle.Modal;

}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

{

using (OpenFileDialog dialog = new OpenFileDialog())

{

dialog.Filter = this.CustomFilter;

if (dialog.ShowDialog() == DialogResult.OK)

value = dialog.FileName;

}

return value;

}

}

The drawback of this approach is that you cannot define the custom filter in design time directly, insteadyou can create a property on your componentand use the IDictionaryService to store it.

If you choose to define a property on your component to store the filter (you'll have to specify on each component even if they are in the same container) you caninsert before theassignment from above (dialog.Filter = this.CustomFilter) the following lines of code:

C#

if (context.Instance is UserControl)

{

this.CustomFilter = (context.Instance as MyUserControl).CustomFilter;

}

Cristian_t  Saturday, April 07, 2007 11:10 AM

You can use google to search for other answers

Custom Search

More Threads

• printPreviewDialog WITHOUT colours
• Enumeration of Virtual Key codes or (convert system.windows.forms.keys to virtual key code)
• Rendering Children Controls
• Control program instance
• Create a new excel document
• Clippping Regions in PaintEventArgs is width=0, height=0?...
• INI files
• build collection, then display in list box in dialog
• Changing a button's background color
• Progress Bars in Status Bar help