| By Xin Yan | Article Rating: |
|
| July 29, 2005 01:45 AM EDT | Reads: |
116,040 |
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:
- 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.
- 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.
Published July 29, 2005 Reads 116,040
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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. 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 |
||||
![]() |
.NET News Desk 07/28/05 02:49:30 PM EDT | |||
Creating Custom Controls for Microsoft .NET Compact Framework in Visual Studio 2005 |
||||
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications




























