Click here to Skip to main content
15,911,707 members
Articles / Programming Languages / C#
Article

Custom Properties and Custom property Editor

Rate me:
Please Sign up or sign in to vote.
1.18/5 (10 votes)
14 Sep 2008CDDL1 min read 37.5K   13   4
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)


Written By
Software Developer (Senior)
India India
Vikas Maan is a software developer for last 4 years working on windows platform with multiple programming languages mainly VC++, C#.net etc.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Tom Costanza21-Sep-11 7:36
Tom Costanza21-Sep-11 7:36 
GeneralMy vote of 2 Pin
Groulien3-May-11 3:24
Groulien3-May-11 3:24 
GeneralMy vote of 1 Pin
Dave Kreskowiak6-Mar-09 3:44
mveDave Kreskowiak6-Mar-09 3:44 
GeneralRe: My vote of 1 Pin
Lord of Scripts20-Jul-09 20:13
Lord of Scripts20-Jul-09 20:13 

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.