Properties Window File Dialog
-
Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.
-
Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.
you can try this sample code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.ComponentModel;namespace testproperty
{
public class TestClass
{
public TestClass()
{
}
private int id;
[System.ComponentModel.Category("Main")]
[System.ComponentModel.DisplayName("ID of Class")]
public int Id
{
get { return id; }
set { id = value; }
}private string fileName; \[System.ComponentModel.Category("Others")\] \[System.ComponentModel.DisplayName("FileName to load")\] \[BrowsableAttribute(true)\] \[EditorAttribute(typeof(OpenFileDialogEditor), typeof(System.Drawing.Design.UITypeEditor))\] public string FileName { get { return fileName; } set { fileName = value; } } } \[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")\] public class OpenFileDialogEditor : System.Drawing.Design.UITypeEditor { public OpenFileDialogEditor() { } public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) { using (System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog()) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { try { ofd.FileName = value.ToString(); } catch { } } if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { return ofd.FileName; } return string.Empty; } } }
}
look the FileName property of TestClass hope it helps
dhaim programming is a hobby that make some money as side effect :)
-
Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.
One of my articles might be able to help you with customizing the design-time events of user controls, it can be found here[^].
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog
-
Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.
VS uses a property grid to show the properties of a control. You have to define the editors to use in your property, so when the grid access your class (by reflection), it knows what editor to use. The editors must be of the type UITypeEditor. You can sub-class it to create your own editors. The file browser is the
System.Windows.Forms.Design.FileNameEditor
class. You define the editor in this way:[Editor(typeof(System.Windows.Forms.Design.FileNameEditor),typeof(System.Drawing.Design.UITypeEditor))]
public string YourProperty
{
...You can find more information on this MSDN article. BTW, you need to link the System.Design library to use the filename editor.