Click here to Skip to main content
6,594,932 members and growing! (16,876 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Creating Custom Controls-Providing Design Time Support 2

By Kodanda Pani

This article explains how we can use Designers for providing Design time support.
C#, Windows, .NET, Visual Studio, Dev
Posted:2 Mar 2005
Views:42,316
Bookmarked:51 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.92 Rating: 3.64 out of 5

1
2 votes, 16.7%
2

3
3 votes, 25.0%
4
7 votes, 58.3%
5

Introduction

In the first article, we explained how we can use TypeConverters and UITypeEditors for providing design time support for custom controls and components. This article explains how we can use Designers for providing design time support.

We know that, a few Windows Forms controls display one or more hyperlinks at the bottom edge of the properties window and additional commands in the context menu. (Example: DataGrid - Format Link, TabControl - Add Tab, Remove Tab). Let�s see how we can create hyperlinks for our custom controls.

.NET Support

The .NET Framework provides interfaces, classes and attributes for providing design time support. IDesigner interface allows us to create designer classes that provide logic that can adjust the appearance or behavior of a type at design time. IDesigner interface has a read-only Verbs property which allows us to create custom menu commands and hyperlinks. This property returns DesignerVerbCollection containing the DesignerVerb objects for generating menu commands.

The ControlDesigner class (implements IDesigner interface) allows us to create custom designers which can be sited on the form. (Otherwise they will appear in the component tray only).

Implementing Custom Designer

The current example creates a custom designer for configuring the appearance of the PhoneNumber custom control. The PhoneNumber control is a custom user control which is used for collecting phone number information.

The first step in creating a custom designer is to create a class that inherits from System.Windows.Forms.Design.ControlDesigner class. (PhoneNumberControlDesigner)

To provide commands in the property window or in the context menu, we need to override the Verbs read-only property.

PhoneNumberControlDesigner class overrides this property to create a new DesignerVerb and adds it to the DesignerVerbCollection.

public override DesignerVerbCollection Verbs
{   
  get{               
       if(actions == null){                     
    actions = new DesignerVerbCollection();                      
    actions.Add(new DesignerVerb("Look and Feel", 
          new EventHandler(ChangeDisplay)));             
    return actions;               
    }          
     }
}

A DesignerVerb is a menu command linked to an event handler. We used two parameter constructors for creating a new DesignerVerb object. The first parameter is the text that is displayed on the property window or on the context menu. The second parameter is the event handler (name) that performs some action for this verb.

Now we need to implement some action for the verb (menu command) in our event handler. The changedisplay event handler creates a new Formater object (using which we can configure the PhoneNumber control) and shows it as model dialogue.

Sample screenshot

It also updates/changes the control�s design time appearance by setting the control�s background color and font properties as per user�s selection.

Formater formt = new Formater();
formt.ShowDialog();

Now we have finished creating our designer class. In the next step, we need to associate this class with our custom user control. Doing this is very easy. We just have to use the Designer attribute.

[Designer(typeof(PhoneNumberControlDesigner))]
public class 
PhoneNumControl : System.Windows.Forms.UserControl{
}

Sample screenshot

That�s all what we have to do. Now it�s the time for compiling and using our code. When we add the PhoneNumber control to any form, a new command �Look and Feel� will be displayed in the context menu. Also a hyperlink is added at the bottom of the Property Editor. Clicking on this hyperlink will display the Formater window using which we can change the format of the control. Formater window contains all the available formatting options and the preview for those options. User can choose among the available formatting options. The format (color, font) of the custom control will be modified accordingly.

Conclusion

These two articles cover how we can provide design support for our controls using TypeConverters, UITypeEditors and Designers.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kodanda Pani


Member
Working on .NET for last 3 years. Currently working for TCS.
Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
AnswerPersisting changes to runtime PinmemberStefan Van Reeth12:02 15 Jan '09  
GeneralThanks a Lot Pinmemberniko7820:22 29 Sep '08  
GeneralLost changes at runtime PinmemberSamuTupe5:52 17 Jan '07  
GeneralRe: Lost changes at runtime PinmemberSamuTupe12:51 18 Jan '07  
Generaladding stuff to designed control [modified] Pinmemberseishin66614:00 18 Nov '06  
QuestionHow to debug Pinmemberhemaral10:41 2 Oct '06  
AnswerRe: How to debug Pinmemberiron1066:24 2 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Mar 2005
Editor: Sumalatha K.R.
Copyright 2005 by Kodanda Pani
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project