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;
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