Hi Colin,
Here is a simple sample for adding a dummy property into the
PropertyCollection.
<code>
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace _DesignTime__ICustomTypeDescriptor_
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class UserControl1 :
System.Windows.Forms.UserControl,ICustomTypeDescriptor
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
#region ICustomTypeDescriptor Members
public TypeConverter GetConverter()
{
// TODO: Add UserControl1.GetConverter implementation
return null;
//return TypeDescriptor.GetConverter(this);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
// TODO: Add UserControl1.GetEvents implementation
return TypeDescriptor.GetEvents(this,attributes,true);
}
EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
// TODO: Add
UserControl1.System.ComponentModel.ICustomTypeDescriptor.GetEvents
implementation
return TypeDescriptor.GetEvents(this,null,true);
}
public string GetComponentName()
{
// TODO: Add UserControl1.GetComponentName implementation
return TypeDescriptor.GetComponentName(this,true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
// TODO: Add UserControl1.GetPropertyOwner implementation
if (pd == null)
return this;
else return pd.GetValue(GetPropertyOwner(pd));
}
public AttributeCollection GetAttributes()
{
// TODO: Add UserControl1.GetAttributes implementation
//return TypeDescriptor.GetAttributes(this,true);
//need return an empty collectiom
return TypeDescriptor.GetAttributes(this,true);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
// TODO: Add UserControl1.GetProperties implementation
return ((ICustomTypeDescriptor)this).GetProperties();
}
PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
// TODO: Add
UserControl1.System.ComponentModel.ICustomTypeDescriptor.GetProperties
implementation
PropertyDescriptorCollection col =
TypeDescriptor.GetProperties(this,null,true);
ArrayList props = new ArrayList(col);
props.Add(new DummyPropertyDescriptor("Dummy"));
PropertyDescriptorCollection newCol = new PropertyDescriptorCollection(
(PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor)));
return newCol;
}
public object GetEditor(Type editorBaseType)
{
// TODO: Add UserControl1.GetEditor implementation
return null;
}
public PropertyDescriptor GetDefaultProperty()
{
// TODO: Add UserControl1.GetDefaultProperty implementation
return TypeDescriptor.GetDefaultProperty(this,true);
}
public EventDescriptor GetDefaultEvent()
{
// TODO: Add UserControl1.GetDefaultEvent implementation
return TypeDescriptor.GetDefaultEvent(this,true);
}
public string GetClassName()
{
// TODO: Add UserControl1.GetClassName implementation
return TypeDescriptor.GetClassName(this,true);
}
#endregion
}
class DummyPropertyDescriptor : PropertyDescriptor
{
public DummyPropertyDescriptor(string name)
:base(name,null)
{
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get
{
return typeof(Component);
}
}
public override object GetValue(object component)
{
return null;
}
public override void SetValue(object component, object value)
{
}
public override Type PropertyType
{
get
{
return typeof(string);
}
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override void ResetValue(object component)
{
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
}
</code>
Hope it helps!
Best regards,
Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.