Welcome!

.NET Authors: Lee Novak, Liz McMillan, Mark O'Neill, Peter Silva, Yakov Werde

Related Topics: .NET

.NET: Article

Creating Controls for.NET Compact Framework in Visual Studio 2005

Learn to build the building blocks of mobile UI

This DesignTimeAttribute.xmta file is where Visual Studio 2005 stores design-time attributes for custom control. This file can be directly edited in XML editor. It has intellisense support so you can find out the full list of design-time attributes.

Compile the project and go to the project output folder. There will be three files:

  • SignInControl.dll
  • SignInControl.pdb
  • SignInControl.Smartphone.asmmeta.dll

You should already be familiar with SignInControl.dll and SignInControl.pdb. What is SignInControl.Smartphone.asmmeta.dll? This is design-time metadata assembly; it is where the design-time attributes reside. Design-time metadata assembly is used by Visual Studio 2005 during design-time. If you run ildasm.exe on it, you will find the Description and Category attributes that we just created.

Now the description and category issue is solved. What about the second one? How to control when to generate code for properties? It can be solved by DefaultValue design-time attribute. Or, it can be done by adding a ShouldSerializeXXX method to the custom control. ShouldSerializeXXX method is very easy to create and it provides more flexibility. Open SignInControl.cs in code editor and add the following code:

private bool ShouldSerializeUserName() {
    return textBox1.Text != "";
}
private bool ShouldSerializePassword() {
    return textBox2.Text != "";
}

private bool ShouldSerializeAnimationMode() {
    return animationMode != AnimationMode.None;
}

private bool ShouldSerializeAnimationImages() {
    return animationImages != null;
}

ShouldSerializeXXX method is invoked by Visual Studio 2005 during design-time to check if property’s value should be persisted into code. By implementing this method for a property, custom control can decide whether a property value should be persisted or not.

Adding Device-Specific Functionalities
SignInControl is intended to be used by Smartphone users. If the password is required to be all numerical (such as PIN number), people using the application will have a hard time because by default Smartphone TextBox takes all characters, both text and numbers. For this reason, I decided to add a “number only” mode to my SignInControl to help the users.

After defining a PasswordInput-Mode enum type with two values – Regular and NumberOnly – I created textBox2.GotFocus event handler (textBox2 is the TextBox for input password) as textBox2_GotFocus and added the code in Listing 3 to SignInControl.cs



This code adds a new property, PasswordInputMode, to SignInControl. If the input mode is set to NumberOnly, whenever textBox2 (the TextBox control for taking password) receives focus, an EM_SETINPUTMODE windows message will be sent to set the input mode to number-only mode. P/Invoke (Platform Invoke) is used here to find the native window handle and send windows messages.

This custom control will work as expected in a Smartphone device or emulator. However, after this change, SignInControl will be replaced by an empty placeholder in the designer as in Figure 7.

This is because Visual Studio 2005 detects the existence of P/Invoke code in the custom control. P/Invoke code is mostly specific to device OS and may fail during design-time when running on a desktop computer. Custom controls with P/Invoke, or referencing assemblies containing P/Invoke, are treated as Device Specific.

To prevent issues from happening during design-time, by default Visual Studio replaces device-specific custom control with a placeholder control in designer. This is why the empty placeholder control appears.

To solve this issue, the key is to:

  1. Make the custom control desktop compatible. Usually this means do not execute the device-specific code during design-time. Or, if executing them cannot be avoided during design-time, handle the error gracefully.
  2. Add DesktopCompatible(true) design-time attribute to the control.

In the case of SignInControl, I need to make sure the textBox2.GotFocus event handler is not being executed during design-time. Luckily this is already done: a TextBox under design mode cannot receive focus. What’s left is to add the DesktopCompatible (true) attribute to mark SignInControl compatible with desktop design-time.

As mentioned earlier, class designer is the best way to add design-time attributes. Open class designer, select SignInControl, and open the editor for Custom Attributes. Type in:

DesktopCompatible(true)

Now compile and reopen the form designer. The custom control is back to the designer!

More Design-time Features You Can Use
There are lots of design-time features in Visual Studio 2005. Some common ones are:

  • Use BrowsableAttribute to hide a property or event from property browser during design-time.
  • Use DesignerSerializationVisibilityAttribute to specify how a property’s value is persisted into code.
  • Use DefaultValueAttribute to specify a default value for the property.
  • Specify an Editor with EditorAttribute. An Editor can customize the property-editing experience during design-time.
  • Specify a Designer with DesignerAttri-bute. A Designer can add context menu options during design-time; it can re-place a property (property shadowing) during design-time, and can change
  • the custom control’s behavior during design-time.

Hope this article gives you some understanding about how to create a .NET Compact Framework custom control in Visual Studio 2005 and provide design-time support. Please take a look at the SignInControl sample used in this article (source code is available by viewing this article online at http://dotnet.sys-con.com/). If you have further questions and comments, please contact our team blog (http://blogs.msdn.com/vsdteam) or e-mail me at xinyan@microsoft.com.

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.