As I mentioned in introducing the Designer post that one of the designer powers is filtering the control members, filtering here means removing or adding members (Properties, Events or Attributes) to the DesignTime Environment in the PropertyWindow.
The IDesignerFilter interface has the methods that enable filtering the members that appear in the Design time PropertyWindow.
The IDesignerFilter interface has 6 methods for filtering.
Why for each Properties , Events and Attributes there is a Pre and Post Filter method?
[more]
From msdn.
If you want to add an attribute or attributes, implement an override of the PreFilterAttributes method that adds the new System..::.Attribute to the IDictionary passed to the method. The keys in the dictionary are the type IDs of the attributes. To change or remove an attribute or attributes, implement an override of the PostFilterAttributes method.
If you want to add an event or events, implement an override of the PreFilterEvents method that adds the new EventDescriptor to the IDictionary passed to the method. The keys in the dictionary are the names of the events. To change or remove an event or events, implement an override of the PostFilterEvents method.
If you want to add a property or properties, implement an override of the PreFilterProperties method that adds the new PropertyDescriptor to the IDictionary passed to the method. The keys in the dictionary are the names of the properties. To change or remove a property or properties, implement an override of the PostFilterProperties method.
Ok now we know why and when we should use these methods, what about a sample. The sample I will introduce here is a designer for a simple UserControl that adds Design Time property to whether show a dotted border or not (to not get lost when not selected) , and will remove some properties too.
[code:c#]
public class UserControlDesigner : ControlDesigner
{
Pen _pen;
Control _control;
bool _showBorder;
public bool ShowBorder //Property to be added to the Property grid
{
get { return _showBorder; }
set { _showBorder = value; }
}
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
_control = this.Control;v _showBorder = true;
_pen = new Pen(Color.Red);
_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
}
protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe)
{
base.OnPaintAdornments(pe);
if (_showBorder && _control != null)
{
pe.Graphics.DrawRectangle(_pen, new Rectangle(0, 0, _control.Width – 1, _control.Height – 1));
}
}
}
[/code]
The Above code is for a designer that adds a red dotted border to a control( useful when used with an empty usercontrol ). Now I will add the ShowBorder property to the set of properties associated with the TestUserControl.
[code:c#]
protected override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties);
//adding the property here
properties.Add("ShowBorder", TypeDescriptor.CreateProperty(typeof(UserControlDesigner), "ShowBorder", typeof(bool), new DescriptionAttribute("This property only appears in design time."), DesignOnlyAttribute.Yes));
}
[/code]
Just note that I added to the created property some attributes to give it too some design time support, AND don't forget to add the DesignOnlyAttribute.Yes to the attributes array of that property so that it doesn't be serialized and you find a compile time error trying to set that property.
[code:c#]
protected override void PostFilterProperties(IDictionary properties)
{
properties.Remove("RightToLeft");
base.PostFilterProperties(properties);
}
[/code]
And here I removed the RightToLeft property. Now all whats left is to associate this designer with the TestUserControl.
[code:c#]
[Designer(typeof(UserControlDesigner))]
public partial class TestUserControl : UserControl
[/code]
Msdn Note:
"When a class extends a designer that implements IDesignerFilter, each PostMethodName method should call the corresponding PostMethodName method of the base class after changing its own attributes, and each PreMethodName method should call the corresponding PreMethodName method of the base class before changing its own attributes."
Thats all see you next part with Verbs
Leave a Reply