/*This code works for me in VS2008. Thank you a lot.
This sample uses a Label control with customized properties. Three properties are added and sorted into a category
Implementing some changes.. you can "filter" the not customized categories:
Create a User Control
Place a Label control
Place a ImageList control and added four images: chat, error, info, lock
*/ See image :
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
namespace STLabel
{
/*Modified by: César F. Qüeb Montejo */
[TypeConverter(typeof(PropertySorter.PropertySorter))]
public partial class STLabel : UserControl
{
public enum EImagenPredeterminada
{
Chat,
Error,
Info,
Protegido
}
private Image _Imagen;
private ContentAlignment _PosImage;
private EImagenPredeterminada _TipoImagen;
private string _Texto;
public STLabel()
{
InitializeComponent();
}
[Description("La imagen a mostrar en la etiqueta o liga")]
[Category("Smartechnology")]
[Browsable(true)]
[DisplayName("Imagen a Mostrar")]
[PropertySorter.PropertyOrder(2)]
public Image ImagenPersonalizada
{
get
{
return _Imagen;
}
set
{
if (_Imagen == value)
return;
_Imagen = value;
STLbl.Image = _Imagen;
}
}
[Description("La posición de la imagen a mostrar en la etiqueta o liga")]
[Category("Smartechnology")]
[Browsable(true)]
[DisplayName("Posición")]
[PropertySorter.PropertyOrder(3)]
public ContentAlignment PosImage
{
get
{
return _PosImage;
}
set
{
if (_PosImage == value)
return;
_PosImage = value;
STLbl.ImageAlign= _PosImage;
}
}
[Description("Imagen predeterminada")]
[Category("Smartechnology")]
[Browsable(true)]
[DisplayName("Tipo de imagen predefinida")]
[PropertySorter.PropertyOrder(1)]
public EImagenPredeterminada TipoImagen
{
get
{
return _TipoImagen;
}
set
{
if (_TipoImagen == value)
return;
_TipoImagen = value;
switch ((int)_TipoImagen)
{
case 0:
ImagenPersonalizada = imageList1.Images["Chat"];
break;
case 1:
ImagenPersonalizada = imageList1.Images["Error"];
break;
case 2:
ImagenPersonalizada = imageList1.Images["Info"];
break;
case 3:
ImagenPersonalizada = imageList1.Images["Lock"];
break;
}
}
}
[Description("El texto a mostrar en la etiqueta")]
[Category("Smartechnology")]
[Browsable(true)]
[DisplayName("Texto a mostrar")]
[PropertySorter.PropertyOrder(4)]
public string Texto
{
get
{
return _Texto;
}
set
{
if (_Texto == value)
return;
_Texto = value;
STLbl.Text = _Texto;
}
}
}
}
namespace PropertySorter
{
public class PropertySorter : ExpandableObjectConverter
{
#region Methods
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
//
// This override returns a list of properties in order
//
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
PropertyDescriptor[] properties = new PropertyDescriptor[] { };
ArrayList orderedProperties = new ArrayList();
/* property at below used to copy ONLY properties in an specific category */ PropertyDescriptorCollection props = new PropertyDescriptorCollection(properties);
foreach (PropertyDescriptor pd in pdc)
{
// used to filter for category
if (pd.Category == "Smartechnology")
{
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
if (attribute != null)
{
//
// If the attribute is found, then create an pair object to hold it
//
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
/* You can pass ONLY the PropertyDescriptor.. but..you know.. i'm testing..*/ /* This line was adapted from original code */ orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order, pd.Category, pd));
}
else
{
//
// If no order attribute is specifed then given it an order of 0
//
/* You can pass ONLY the PropertyDescriptor.. but..you know.. i'm testing..*/ /* This line was adapted from original code */ orderedProperties.Add(new PropertyOrderPair(pd.Name, 0, pd.Category, pd));
}
}
}
//
// Perform the actual order using the value PropertyOrderPair classes
// implementation of IComparable to sort
//
orderedProperties.Sort();
//
// Build a string list of the ordered names
//
ArrayList propertyNames = new ArrayList();
foreach (PropertyOrderPair pop in orderedProperties)
{
/* !!!! The FILTER is implemented here !!!! */
/* TYPE your OWN category */
if (pop.Category.ToLower() == "smartechnology")
{
propertyNames.Add(pop.Name);
props.Add(pop.PropDescriptor);
}
}
//
// Pass in the ordered list for the PropertyDescriptorCollection to sort by
//
/* We are returning the personalized properties only */ return props.Sort((string[])propertyNames.ToArray(typeof(string)));
}
#endregion
}
#region Helper Class - PropertyOrderAttribute
[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
//
// Simple attribute to allow the order of a property to be specified
//
private int _order;
public PropertyOrderAttribute(int order)
{
_order = order;
}
public int Order
{
get
{
return _order;
}
}
}
#endregion
#region Helper Class - PropertyOrderPair
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
/* Following two members added by CFQüeb */ private string _category;
private PropertyDescriptor _propDescriptor;
public string Name
{
get
{
return _name;
}
}
/* Following property added since original code */ public string Category
{
get
{
return _category;
}
set
{
if (_category == value)
return;
_category = value;
}
}
/* Following property added since original code */ public PropertyDescriptor PropDescriptor
{
get
{
return _propDescriptor;
}
set
{
if (_propDescriptor == value)
return;
_propDescriptor = value;
}
}
/* Modified constructor */ public PropertyOrderPair(string name, int order, string category, PropertyDescriptor propdesc)
{
_order = order;
_name = name;
/* next two members were added by CFQüeb */ _category = category;
_propDescriptor = propdesc;
}
public int CompareTo(object obj)
{
//
// Sort the pair objects by ordering by order value
// Equal values get the same rank
//
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
//
// If order not specified, sort by name
//
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name, otherName);
}
else if (otherOrder > _order)
{
return -1;
}
return 1;
}
}
#endregion
}
/*
Using this code... you get the properties sorted and all other categories from class base (inherited) discarted Enjoy!!!!
César F. Qüeb Montejo */