Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Kevin Benedict

Related Topics: .NET

.NET: Article

Creating Controls for.NET Compact Framework in Visual Studio 2005

Learn to build the building blocks of mobile UI

Besides using code editor, there is another (and easier) way to add properties. It is to use class designer. Right click SignInControl.cs node in the solution explorer, select “View Class Diagram,” and you will see class designer. Class designer gives a graphics representation of the code, and it allows adding, updating, and removing types, properties, methods, and events. Figure 4 is a screen shot of SignInControl in class designer.


To test this custom control, compile the project and add a SmartPhone Windows Mobile 5 WinForm application to the solution. When the form is opened, the SignInControl will be available in the toolbox and can be dropped to the form. If animation is needed, set AnimationMode property to On, add an ImageList to the form, and assign the ImageList to SignInControl’s AnimationImage property. Figure 5 is a screenshot of using SignInControl in designer.



Provide Design-Time Support

By now a functional custom control is created. It works on a Smartphone device, it appears in Visual Studio 2005 Toolbox and it can be dragged and dropped to the form designer. It looks pretty good but still misses some useful design-time features:

  1. Properties I added appear in the “Misc” category in the property browser (see Figure 5). Also they do not have descriptions associated with it.
  2. If user does not change properties such as UserName and Password in the designer, the following code will be generated inside InitializeComponent for the user:
this.signInControl1.Password = "";
this.signInControl1.UserName = "";

This is unnecessary because values of both properties are defaulted to the empty string. Ideally, the above code should not be generated.

These two are both design-time requirements. Most of the design-time support is specified in the form of design-time attributes. A design-time attribute describes how a control will behave at design-time. Some common design-time attributes are:

  • BrowsableAttribute
  • CategoryAttribute
  • DescriptionAttribute
  • EditorAttribute
  • DesignerSerializationVisibility-Attribute
  • And more …

You can find the definition of these attributes from MSDN. In the case of SignInControl, CategoryAttribute, and Descrip-tionAttribute need to be specified for each property to put it in the right category, and give it description.

I found the easiest approach to add design-time attributes is to use class designer. Open class designer as in Figure 4 and select desired property (for example UserName). Notice there is a “Custom Attribute” property in the property browser. Click on its editor button (the “…” button) and custom attribute editor will pop up. Type in the design-time attributes (see Figure 6).



Click OK and repeat the same process for other properties. By now Category and Description design-time attributes are added. Take a look at the solution explorer. A new file named DesignTimeAttributes.xmta is added to the project with the following content supported (see Listing 2).

More Stories By Xin Yan

Xin Yan has been a software design engineer at Microsoft for over 7 years. He works on Visual Studio developer tools platform team.

Comments (4) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Jay Dixit 10/11/07 07:07:23 AM EDT

Hi truly a great article but i am new for it and just have some queries.
1. I don't understand AnimationMode.
2. I don't understand PasswordInputMode.
As per the understanding i have gone throught the detail and implemented the same. After leaving AnimationMode and PasswordInputMode, i have run the code on emulator but i am not able to see the effect.

Please help me for this.

Jacques Herweijer 02/05/07 03:23:28 PM EST

Hi great article, I completely understand the usercontrols now in a few minutes, its better than reading a book about it ! That however leaves me with 2 unanswered questions hope you can answer them.

My first question is, How can I access the properties of the labels as soon as the usercontrol is used on a form, so I can change the Text property depending on the form I am using it on, or do I have to create a new usercontrol that inhertis your example for each label Text I want to use and drop that one on my form. I think this is very much work for just setting a label.

And then my second one, How can I add controls to the usercontrol when I put it on my form ? For example if I wanted to add a button to the usercontrol when it is on my form but not in de usercontrol class itself (like a panel can contain controls) in Design-time ofcourse.

Hope you can and be willing to give me an answer because I cannot find these answers anywhere. I think its hard to search the web when some functionallity is not available for Smart Device projects.

Anyway Great article that give me a head start.

.NET News Desk 07/28/05 02:49:36 PM EDT

Creating Custom Controls for Microsoft .NET Compact Framework in Visual Studio 2005
Visual Studio 2005 fully supports creating custom controls in .NET Compact Framework. In addition, custom control developers can create a great design-time experience for custom control users. Design-time support makes it easier for developers to use custom controls in Visual Studio 2005 Smart Device projects.

.NET News Desk 07/28/05 02:49:30 PM EDT

Creating Custom Controls for Microsoft .NET Compact Framework in Visual Studio 2005
Visual Studio 2005 fully supports creating custom controls in .NET Compact Framework. In addition, custom control developers can create a great design-time experience for custom control users. Design-time support makes it easier for developers to use custom controls in Visual Studio 2005 Smart Device projects.