|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHope you are familiar with Windows Custom PropertyFirst, I created one
The constructor looks like this: public CustomProperty(string sName,
object value, bool bReadOnly, bool bVisible )
{
this.sName = sName;
this.objValue = value;
this.bReadOnly = bReadOnly;
this.bVisible = bVisible;
}
Custom PropertyDescriptorNext step is to create a public class CustomPropertyDescriptor: PropertyDescriptor
{
CustomProperty m_Property;
public CustomPropertyDescriptor(ref CustomProperty myProperty,
Attribute [] attrs) :base(myProperty.Name, attrs)
{
m_Property = myProperty;
}
.....
We have to override all methods of Custom ClassWe will be creating object of this public class CustomClass: CollectionBase,ICustomTypeDescriptor
{
public void Add(CustomProperty Value)
{
base.List.Add(Value);
}
public void Remove(string Name)
{
foreach(CustomProperty prop in base.List)
{
if(prop.Name == Name)
{
base.List.Remove(prop);
return;
}
}
}
public CustomProperty this[int index]
{
get
{
return (CustomProperty)base.List[index];
}
set
{
base.List[index] = (CustomProperty)value;
}
}
.......
Here is the most important method public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptor[] newProps = new PropertyDescriptor[this.Count];
for (int i = 0; i < this.Count; i++)
{
CustomProperty prop = (CustomProperty) this[i];
newProps[i] = new CustomPropertyDescriptor(ref prop, attributes);
}
return new PropertyDescriptorCollection(newProps);
}
How to use CustomClass
CustomClass myProperties = new CustomClass();
propertyGrid1.SelectedObject = myProperties;
......
CustomProperty myProp = new CustomProperty(txtName.Text,txtValue.Text,
chkReadOnly.Checked,true);
myProperties.Add(myProp);
propertyGrid1.Refresh();
FinallyI hope users can extend my
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||