- The ControlDesigner Base Class
- DesignerAttribute
- The IconButton Designer
- Adding a Toolbox Bitmap
- Summary
The IconButton Designer
It's time to create a simple designer for the IconButton control. The designer will be built in two stages. The first stage of the designer will filter properties of the control to remove the BackColor and BackgroundImage properties. The next stage of development will introduce the concept of verbs; verbs are actions that can be associated with a control.
As with any project, the first step involves setting up the development environment. After the VS .NET Solution is created, the process of creating and testing the IconButtonDesigner can begin.
Setting Up the SAMS.ToolKit Solution
Before we venture into designer development, now would be a good time to set up a VS .NET Solution that will be used throughout the remainder of the book. In VS .NET a Solution is used to contain one or more related projects. For those of you familiar with Visual Studio 6, a Solution is orthogonal to a workspace.
Start by creating a new C# class library with the name SAMS.ToolKit. This will create a new VS .NET Solution, and the output when compiling the Solution will be SAMS.ToolKit.dll. In addition, the default namespace will also be SAMS.ToolKit.
With the Solution in place, create two folders:
Controls
Design
The new Solution should look similar to what's shown in Figure 3.2.
Figure 3.2 The SAMS.ToolKit Solution.
As with any .NET project, the familiar References folder and the AssemblyInfo.cs source file are automatically created. The folders within the Solution allow for a convenient way to organize code within the project. In addition, any new classes that are created within the folders will have the folder name added to the default namespace.
The Controls folder will need to contain the IconButton.cs file that was created in the preceding chapter. Right-click the Controls folder, select Add Existing Item from the Add menu, and locate the IconButton.cs source file. It is important to note that this operation will copy the source file to the new destination rather than referencing it. This means that there will be two copies of the source and changes to the new source will not be reflected in the original source. Open the IconButton.cs source file and change the namespace to SAMS.ToolKit.Controls.
Filtering Properties
During development of a new custom control, it is sometimes necessary to remove any unwanted or unneeded properties inherited from the base class from which the new custom control derives. The process of adding or removing properties and events is known as filtering. The reason behind filtering, in this case filtering properties, is to alter the available options during the design of the control rather than to provide unnecessary or unused properties/events.
The first designer will be used to remove or filter out two properties from the IconButton: BackColor and BackgroundImage. These properties are inherited from the Control base class and serve no purpose for the IconButton control because neither of these properties has any effect on the control.
The capability to filter properties, events, and attributes comes from implementing the IDesignerFilter interface. Table 3.1 lists the IDesignerFilter interface methods.
Table 3.1 The IDesignerFilter Interface Methods
Method |
Description |
PostFilterAttributes |
Allows a designer to change or remove attributes. |
PostFilterEvents |
Allows a designer to change or remove events. |
PostFilterProperties |
Allows a designer to change or remove properties. |
PreFilterAttributes |
Allows a designer to add attributes. |
PreFilterEvents |
Allows a designer to add events. |
PreFilterProperties |
Allows a designer to add properties. |
Advanced uses of the IDesignerFilter interface are covered in Chapter 5, "Advanced Control Development."
As the first venture into developing a designer, the first pass of the IconButton designer will remove the unused properties BackColor and BackgroundImage. Currently, the IconButton provides both of these properties as they are implemented by the Control base class. The default properties are supplied when the control is created and can be seen in the property grid when the control is selected on the form (see Figure 3.3).
Figure 3.3 The IconButton default properties.
Note
The BackgroundImage property has the value of (none). This means that currently there is no image associated with this property. One of the responsibilities of a Designer class is to provide such feedback to the developer and to the property grid.
Notice the BackColor and BackgroundImage properties displayed in Figure 3.3. To remove these properties, the IconButtonDesigner class will implement the method PostFilterProperties and remove the unwanted properties from the properties collection. Because the ControlDesigner base class implements the IDesignerFilter interface, the IconButtonDesigner class needs to override the implementation of the PostFilterProperties method. Listing 3.1 contains the C# source for the IconButtonDesigner.
Listing 3.1 Designer Stage One
1: //////////////////////////////////////////////////////////////////////// 2: ///File :IconButton.cs 3: ///Author :Richard L. Weeks 4: /// 5: /// Copyright (c) 2001 by Richard L. Weeks 6: /// This file is provided for instructional purposes only. 7: /// No warranties. 8: //////////////////////////////////////////////////////////////////////// 9: 10: using System; 11: using System.ComponentModel; 12: using System.ComponentModel.Design; 13: using System.Collections; 14: using System.Drawing; 15: 16: 17: namespace SAMS.ToolKit.Design 18: { 19: /// <summary> 20: /// Simple Designer for IconButton 21: /// </summary> 22: public class IconButtonDesigner : _System.Windows.Forms.Design.ControlDesigner { 23: 24: 25: 26: public IconButtonDesigner() { 27: } 28: 29: 30: //Overrides 31: 32: /// <summary> 33: /// Remove some basic properties that are not supported by the _IconButton 34: /// </summary> 35: /// <param name="Properties"></param> 36: protected override void PostFilterProperties( _IDictionary Properties ) { 37: Properties.Remove( "BackgroundImage" ); 38: Properties.Remove( "BackColor" ); 39: } 40: 41: 42: 43: } 44: }
The PostFilterProperties method receives an IDictionary interface to a collection of properties associated with the control being designed. As with any collection, the Remove method is used to remove the specified item from the collection. In the case of the IconButtonDesigner, the code on lines 37 and 38 of Listing 3.1 remove or filter out the unwated properties: BackgroundImage and BackColor.
With the unwanted properties filtered out, they will no longer be displayed within the property grid during the design-time of the IconButton control. However, pragmatic access to the properties is still available to the developer.
To enable the designer for the IconButton control, add the following attribute to the IconButton class:
System.ComponentModel.Designer(typeof(SAMS.ToolKit.Design.IconButtonDesigner))
The IconButton class should now look similar to what's shown in Listing 3.2.
Listing 3.2 Updated Attributes for the IconButton
1:[ 2:System.ComponentModel.Description( "SAMS IconButton Control" ), 3:System.ComponentModel.Designer( _ typeof( SAMS.ToolKit.Design.IconButtonDesigner ) ) 4:] 5:public class IconButton : System.Windows.Forms.Control { 6: //IconButton implementation 7: }
Rebuild the SAMS.ToolKit Solution to produce the new control library. To test the results of the designer, start a new Windows Forms Solution and add the IconButton to the form. Notice that the BackColor and BackgroundImage properties are no longer displayed in the property grid, as shown in Figure 3.4.
Figure 3.4 The first phase of the IconButtonDesigner.
Designer Verbs
Verbs are best described as actions that can be applied to the control being designed. Verbs for a control are linked to an event handler and are added to the context menu for the control, as well as the property window. The best way to understand the role of verbs is to implement them, and that's exactly what the second phase of the IconButtonDesigner is about.
To support adding verbs for a control, the designer needs to implement the Verbs property. The Verbs property returns a DesignerVerbsCollection of DesignerVerbs that the control designer supports. The IconButtonDesigner will be extended to provide verbs for changing the ForeColor property of the control to Red, Green, or Blue. The event handler for custom verbs uses the following EventHandler signature:
void EventHandler( object sender, EventArgs e )
Listing 3.3 shows the updated IconButtonDesigner with the Verbs property implemented.
Listing 3.3 Designer Stage Two
1: //////////////////////////////////////////////////////////////////////// 2: ///File :IconButton.cs 3: ///Author :Richard L. Weeks 4: /// 5: /// Copyright (c) 2001 by Richard L. Weeks 6: /// This file is provided for instructional purposes only. 7: /// No warranties. 8: //////////////////////////////////////////////////////////////////////// 9: 10: using System; 11: using System.ComponentModel; 12: using System.ComponentModel.Design; 13: using System.Collections; 14: using System.Drawing; 15: 16: 17: namespace SAMS.ToolKit.Design 18: { 19: /// <summary> 20: /// Simple Designer for IconButton 21: /// </summary> 22: public class IconButtonDesigner : _System.Windows.Forms.Design.ControlDesigner { 23: 24: 25: 26: public IconButtonDesigner() { 27: } 28: 29: public override DesignerVerbCollection Verbs { 30: get { 31: DesignerVerb[] verbs = new DesignerVerb[3]; 32: verbs[0] = new DesignerVerb( "Red", _new EventHandler( this.OnRedVerb ) ); 33: verbs[1] = new DesignerVerb( "Green", _new EventHandler( this.OnGreenVerb ) ); 34: verbs[2] = new DesignerVerb( "Blue", _new EventHandler( this.OnBlueVerb ) ); 35: return new DesignerVerbCollection( verbs ); 36: } 37: } 38: 39: 40: //Overrides 41: 42: /// <summary> 43: /// Remove some basic properties that are not supported by the _IconButton 44: /// </summary> 45: /// <param name="Properties"></param> 46: protected override void PostFilterProperties( _IDictionary Properties ) { 47: Properties.Remove( "BackgroundImage" ); 48: Properties.Remove( "BackColor" ); 49: } 50: 51: 52: //Verb Handlers 53: protected void OnRedVerb( object sender, EventArgs e ) { 54: this.Control.ForeColor = System.Drawing.Color.Red; 55: } 56: protected void OnGreenVerb( object sender, EventArgs e ) { 57: this.Control.ForeColor = System.Drawing.Color.Green; 58: } 59: protected void OnBlueVerb( object sender, EventArgs e ) { 60: this.Control.ForeColor = System.Drawing.Color.Blue; 61: } 62: 63: 64: } 65: }
Line 29 of Listing 3.3 implements the Verbs property. Each verb defines a text string for the menu and an EventHandler to be invoked when the menu handler is selected. Figure 3.5 shows the context menu and property grid of the IconButton using the revised IconButtonDesigner class.
Figure 3.5 Verbs support.
VS .NET handles the context menu and property grid support for displaying the supported verbs or commands that the current control designer supports. When one of the supported verbs is selected, the designated EventHandler is invoked so that the verb can be executed. In the case of the IconButtonDesigner, the ForeColor property of the IconButton being designed is updated accordingly.
Designer verbs also allow for user feedback such as providing a check mark for the current ForeColor selected. To provide this feedback, the Checked property of the DesignerVerb item needs to be set. The current implementation of the IconButtonDesigner merely creates the supported designer verbs within the context of the Verbs property rather than as an implementation member. Listing 3.4 updates the IconButtonDesigner to support providing the DesignerVerbs as members and makes use of the Checked property.
Listing 3.4 The Updated IconButtonDesigner Class
1: //////////////////////////////////////////////////////////////////////// 2: ///File :IconButton.cs 3: ///Author :Richard L. Weeks 4: /// 5: /// Copyright (c) 2001 by Richard L. Weeks 6: /// This file is provided for instructional purposes only. 7: /// No warranties. 8: //////////////////////////////////////////////////////////////////////// 9: 10: using System; 11: using System.ComponentModel; 12: using System.ComponentModel.Design; 13: using System.Collections; 14: using System.Drawing; 15: 16: 17: namespace SAMS.ToolKit.Design 18: { 19: /// <summary> 20: /// Simple Designer for IconButton 21: /// </summary> 22: public class IconButtonDesigner : _System.Windows.Forms.Design.ControlDesigner { 23: 24: private enum VERBS { 25: Red, 26: Green, 27: Blue 28: } 29: 30: private DesignerVerb[] designerVerbs; 31: 32: public IconButtonDesigner() { 33: designerVerbs = new DesignerVerb[3]; 34: DesignerVerb[] verbs = new DesignerVerb[3]; 35: designerVerbs[(int)VERBS.Red] = _new DesignerVerb( "Red", new EventHandler( this.OnRedVerb ) ); 36: designerVerbs[(int)VERBS.Green] = _new DesignerVerb( "Green", new EventHandler( this.OnGreenVerb ) ); 37: designerVerbs[(int)VERBS.Blue] = _new DesignerVerb( "Blue", new EventHandler( this.OnBlueVerb ) ); 38: } 39: 40: public override DesignerVerbCollection Verbs { 41: get { 42: return new DesignerVerbCollection( designerVerbs ); 43: } 44: } 45: 46: 47: //Overrides 48: 49: /// <summary> 50: /// Remove some basic properties that are not supported by the _IconButton 51: /// </summary> 52: /// <param name="Properties"></param> 53: protected override void PostFilterProperties( _ IDictionary Properties ) { 54: Properties.Remove( "BackgroundImage" ); 55: Properties.Remove( "BackColor" ); 56: } 57: 58: 59: //Verb Handlers 60: protected void OnRedVerb( object sender, EventArgs e ) { 61: this.Control.ForeColor = System.Drawing.Color.Red; 62: UpdateCheckMarks( VERBS.Red ); 63: } 64: protected void OnGreenVerb( object sender, EventArgs e ) { 65: this.Control.ForeColor = System.Drawing.Color.Green; 66: UpdateCheckMarks( VERBS.Green ); 67: } 68: protected void OnBlueVerb( object sender, EventArgs e ) { 69: this.Control.ForeColor = System.Drawing.Color.Blue; 70: UpdateCheckMarks( VERBS.Blue ); 71: } 72: 73: 74: private void UpdateCheckMarks( VERBS ActiveVerb ) { 75: foreach( DesignerVerb dv in designerVerbs ) 76: dv.Checked = false; 77: designerVerbs[ (int)ActiveVerb ].Checked = true; 78: } 79: } 80: }
As a result of the updated IconButtonDesigner, the custom verbs on the context menu will show a check mark next to the currently selected foreground color corresponding to the selected verb (see Figure 3.6).
With the addition of verbs, the IconButtonDesigner class is beginning to take shape. In Chapter 5, "Advanced Control Development," the designer will be extended to provide even more features. By now you should have the basic idea of what is involved in developing a designer.
Figure 3.6 Using the designer verb Checked