Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Adding a File Open dialog to a PropertyGrid control

Rate me:
Please Sign up or sign in to vote.
4.79/5 (12 votes)
8 May 2011CPOL1 min read 40.8K   6   4
The Visual Studio Property pane is a control, which can be used in WinForms like any other, and it provides a simple, easy to use property editor for your classes. However, it is not obvious how to get it to open a browse dialog for a file path property.
If you want to allow the user access to your class properties, you could display them in a grid of some kind, or design a form to access them. But, as you probably know, there is a simpler way: just drop a PropertyGrid control on your form, and set it's SelectedObject property at run time. It then automatically fills with names, and contents, and allows the user to edit them. This is very handy, but there are a few limitations:

1) It really doesn't handle Collections well: There seems to be no way to get a notification that the collection content has changed, and the "Add" button in the Collections editor will throw an trapped exception for "constructor not found" even for basic types like System.String. You can get round this with a custom Collections editor, but...
2) You have to tell it "this is a file", and it is not easy to find out how to do it.

It took me around eight hours of Google / MSDN / Fiddle / swear (repeat) to work it out, but it is very simple.. Create a new WinForms project, call your form "frmMain" and drop a PropertyGrid control onto it. Rename the control "pgDetails" then:
C#
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace PropertyGridFileBrowser
    {
    public partial class frmMain : Form
        {
        private MyTestClass tester = new MyTestClass();
        public frmMain()
            {
            InitializeComponent();
            }
        private void frmMain_Load(object sender, EventArgs e)
            {
            tester.Filename = @"F:\Temp\Blue.bmp";
            pgDetails.SelectedObject = tester;
            }
        }
    public class MyTestClass
        {
        [Category("File")]
        [Description("Source file for thumbnail and web images")]
        [EditorAttribute(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Filename { get; set; }
        public MyTestClass() { }
        }
    }
That's it: all code done. Just add the "EditorAttribute" line to each file based property. The only other thing you have to do is add a reference to "System.Design" to your project.

Remember: A PropertyGrid shows all properties with a Public getter: Private, Internal, and Protected are not shown. It allows the user to change any property which has a Public setter - Private, Internal and Protected setters cause read-only properties in the control.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
GeneralMy vote of 5 Pin
sumitkm29-Sep-12 7:46
sumitkm29-Sep-12 7:46 
QuestionError (type or namespace does not exist) Pin
Niyazi Yarar24-Jul-12 8:36
Niyazi Yarar24-Jul-12 8:36 
AnswerRe: Error (type or namespace does not exist) Pin
OriginalGriff24-Jul-12 8:51
mveOriginalGriff24-Jul-12 8:51 
GeneralVery interesting solution, thanks for sharing! Pin
DrABELL10-May-11 10:37
DrABELL10-May-11 10:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.