How can I get an OpenFileDialog in a custom control's property grid?
Asked Answered
O

3

12

I'm creating a .net custom control and it should be able to load multiple text files. I have a public property named ListFiles with those properties set :


[Browsable(true), Category("Configuration"), Description("List of Files to Load")]
public string ListFiles
  {
     get { return m_oList; }
     set { m_oList = value; }
  }

Depending upon the type of object, (string, string[], List, ...), the property grid will allow the user to enter some data.. My goal would be to have a filtered openfiledialog in the Properties Grid of my component that would enable the user to choose multiple files and return it as an array or string (or something else...).

Sooo... Here's my question : How can I get an OpenFileDialog in a custom control's property grid?

Thanks a lot!

Ogilvy answered 4/10, 2008 at 18:16 Comment(0)
L
11

You can do this by adding a UITypeEditor.

Here is an example of a UITypeEditor that gives you the OpenFileDialog for chossing a filename.

Lugsail answered 4/10, 2008 at 18:32 Comment(0)
C
17

You can use built-in UITypeEditor. It is called FileNameEditor

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

public string SomeFilePath
{
 get;
 set;
}
Coltin answered 3/3, 2010 at 17:13 Comment(1)
You will need to add a System.Design.dll reference. The System.Design.dll is only available if you are targeting .net framework 4.0 (full), not 4.0 client profileErgograph
L
11

You can do this by adding a UITypeEditor.

Here is an example of a UITypeEditor that gives you the OpenFileDialog for chossing a filename.

Lugsail answered 4/10, 2008 at 18:32 Comment(0)
T
2

Here's another example comes with customizing File Dialog :

CustomFileEditor.cs

using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace YourNameSpace
{
    class CustomFileBrowser : FileNameEditor
    {
        protected override void InitializeDialog(OpenFileDialog openFileDialog)
        {
            base.InitializeDialog(openFileDialog);
            openFileDialog.Title = "Select Project File : ";
            openFileDialog.Filter = "Project File (*.proj)|*.proj"; ;
        }
    }

}

Usage :

            [Category("Settings"), DisplayName("Project File:")]
            [EditorAttribute(typeof(CustomFileBrowser), typeof(System.Drawing.Design.UITypeEditor))]
            public string Project_File { get; set; }
Tripletail answered 9/5, 2019 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.