Give your Custom Controls a Design-Time Support part2
Hi again,
In part 1 I talked about the components that work together to give a design-time support for your custom controls.
In this part I will crack in some details, I will start with the UI Type Editors,
First for people who don't know what is an UI Type Editor , it is the editor that appears in the property grid window, it is the part where you actually assign the property with a value, the default editor is just a white space to enter your value like this:
just a plain space as you see to put your value in, the visual studio is kind enough to give us some default editors for our properties having some known types for example , bool : your boolean properties automatic will be given the default editor of boolean types which is a small combo having two values true and false, one more complex editor visual studio gives is for properties of type Color: are given a palette of colors(custom,web,system), some other editors are not in place which means they don't appear as if they were from a dropdown window for example when you edit the Items collection of a ListView or TabPages collection of TabControl those properties when edited a popup window appears (a dialog) to give more options handling complex Properties ,. I think that was enough for an introduction and lets see how we make some for our Controls.
And now for some real code, how to make a uitypeEditor for your control.
You have the following tasks to follow:
1- Of course have the Custom Control that has a property that needs a sepcial editor.
2-Implement a class that Derives from System.Drawing.Design.UITypeEditor.
3-You need now to override some methods:
3.1-First one which tells the type of the Editor DropDown, Modal.None
public override UITypeEditorEditStyle GetEditStyle(
System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
Very simple you just return a value of type UIEditorEditStyle and thats it.
3.2-Next one is to tell the design time environment interacts with the user interface of our small editor,this example I give here is making the editor of a property of Type int to appear as a Vertical TrackBar and read its value to the property
which can be used later as the editor in my ProgressDisk Control of the SquareSize property.
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService iwfes =(IWindowsFormsEditorService)provider.GetService( typeof(IWindowsFormsEditorService));
if (iwfes == null)
{
return null;
}
TrackBar trackBar = new TrackBar();
trackBar.Orientation = Orientation.Vertical;
trackBar.SetRange(30, 90);
trackBar.Size = new Size(50, 100);
iwfes.DropDownControl(trackBar);
Size theSize= new Size(trackBar.Value, trackBar.Value);
return theSize;
}
The trick here is to add a control to the IWindowsFormsEditorService reference, in the example I added a trackbar for more complex scenarios you can make a UserControl to take care of more options as Drawing itself having more controls as this one:
3.3-UITypeEditor has two more methods that can be overriden to support some painting in the editor are itself as this:
These 2 methods are:
3.3.1-GetPaintValueSupported and this is to tell the Designer that the Editor Supports Drawing
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
3.3.2-PaintValue which paints the part giving event args e having 4 main properties
Value: the current value of the property.
Graphics: graphics object used to draw with.
Bounds: the rectangle bound to draw on.
Context: gets the current context.
public override void PaintValue(PaintValueEventArgs e)
{
string bmpName = null;
int theSize = (int)pe.Value;
if (theSize > 80)
{
bmpName = "best.bmp";
}
else if (theSize > 60)
{
bmpName = "ok.bmp";
}
else
{
bmpName = "bad.bmp";
}
Bitmap b = new Bitmap(typeof(SizeEditor), bmpName);
pe.Graphics.DrawImage(b, pe.Bounds);
b.Dispose();
}
4-Now the only thing left is to use the appropriate Attribute that attaches the UITypeEditor we just made to the Property as
[EditorAttribute(typeof(SizeEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public int BlockSize
{
...
}
All these methods of course are optional choose which methods you need to override according to your Properties needs and good luck.
For some code samples I am working these days on a collection of commonly used Editors I will post all soon after this series, describing the function of each.
Now this part has come to the end, be welcomed for any questions , discussions or comments, thanks.
Wait for the next part ...

0 comments:
Post a Comment