Welcome!

Symbian Authors: Jack Newton, Kevin Benedict, Matthew Lobas, Shelly Palmer, RealWire News Distribution

Related Topics: Microsoft Cloud

Microsoft Cloud: 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 [email protected].

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)

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.


IoT & Smart Cities Stories
Atmosera delivers modern cloud services that maximize the advantages of cloud-based infrastructures. Offering private, hybrid, and public cloud solutions, Atmosera works closely with customers to engineer, deploy, and operate cloud architectures with advanced services that deliver strategic business outcomes. Atmosera's expertise simplifies the process of cloud transformation and our 20+ years of experience managing complex IT environments provides our customers with the confidence and trust tha...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
Intel is an American multinational corporation and technology company headquartered in Santa Clara, California, in the Silicon Valley. It is the world's second largest and second highest valued semiconductor chip maker based on revenue after being overtaken by Samsung, and is the inventor of the x86 series of microprocessors, the processors found in most personal computers (PCs). Intel supplies processors for computer system manufacturers such as Apple, Lenovo, HP, and Dell. Intel also manufactu...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
OpsRamp is an enterprise IT operation platform provided by US-based OpsRamp, Inc. It provides SaaS services through support for increasingly complex cloud and hybrid computing environments from system operation to service management. The OpsRamp platform is a SaaS-based, multi-tenant solution that enables enterprise IT organizations and cloud service providers like JBS the flexibility and control they need to manage and monitor today's hybrid, multi-cloud infrastructure, applications, and wor...
The Master of Science in Artificial Intelligence (MSAI) provides a comprehensive framework of theory and practice in the emerging field of AI. The program delivers the foundational knowledge needed to explore both key contextual areas and complex technical applications of AI systems. Curriculum incorporates elements of data science, robotics, and machine learning-enabling you to pursue a holistic and interdisciplinary course of study while preparing for a position in AI research, operations, ...
CloudEXPO has been the M&A capital for Cloud companies for more than a decade with memorable acquisition news stories which came out of CloudEXPO expo floor. DevOpsSUMMIT New York faculty member Greg Bledsoe shared his views on IBM's Red Hat acquisition live from NASDAQ floor. Acquisition news was announced during CloudEXPO New York which took place November 12-13, 2019 in New York City.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
Tapping into blockchain revolution early enough translates into a substantial business competitiveness advantage. Codete comprehensively develops custom, blockchain-based business solutions, founded on the most advanced cryptographic innovations, and striking a balance point between complexity of the technologies used in quickly-changing stack building, business impact, and cost-effectiveness. Codete researches and provides business consultancy in the field of single most thrilling innovative te...