Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Custom Properties and Custom property Editor

1.18/5 (10 votes)
15 Sep 2008CDDL1 min read 1  
Define your own property editor for your user control properties

Introduction

This article helps you to define a custom data type property for any user control and custom property editor too. If you take example of property type string[] and open the property in property editor if shows a browse(...) button next to property name. Click on browse button opens an editor to allow user to enter number of strings. Similarly for example if user wants a property of hierarchical list of custom data type then Tree node editor is the best editor for that property. But tree node editor is exclusively meant for TreeNodeCollection properties. TreeNodeCollection class is a sealed class and you cannot instantiate this class and cannot use it externally.

Article Details

So to show the hierarchical list of items user can create his own custom editor. I will take an example here.

Custom Data type:

class DateOfBirth

{

int date;

int month;

int year;

DateOfBirth[] childrenDOB;

}

Property

DateOfBirth[] familyDOB;

Property Editor

So custom editor should be capable of showing the full hierarchy of familyDOB. Each item of familyDOB array as root items in treeView and child items of each item as child node in treeview of their respective parent nodes.

Implementation

public class DateOfBirth
    {
        int date;
        int month;
        int year;
        DateOfBirth[] childrenDOB;
    }

    public class DOBControl : UserControl
    {
        private DateOfBirth[] familyDOB;

        [Editor(typeof(DOBEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public DateOfBirth[] FamilyDOB
        {
            get { return familyDOB; }
            set { familyDOB = value; }
        }

        public object PropertyExplorer(object value)
        {
            DateOfBirth[] _familyDOB=(DateOfBirth[])(value);
            DateOfBirth[] _tempDOB = null;

            //Create the custom property editor instance here
            //custom property editor is exactly similar to default tree node editor
            // 
            //Recursively load nodes and their child nodes in tree view 
            //
            //User can add/remove any nodes in tree view
            //

            //After user closes the property editor here it comes
            //Read all the nodes recursively and create DateOfBirth[] 
            // save in _tempDOB of type DateOfBirth[]

            return _tempDOB;
        }
    }

    public class DOBEditor : System.Drawing.Design.UITypeEditor
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            DOBControl dobControl = (DOBControl)context.Instance;
            return dobControl.PropertyExplorer(value);
        }
    } 

Further Explanation

I am keeping this article very short and not explaining in depth to make it CCC. This is done for one sample data type and could be done for any data type. I will try to upload all required code and a sample also in next update.

Author

Vikas Maan, Tektronix, India

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)