Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020