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) 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.

@ThingsExpo Stories
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...
What is the best strategy for selecting the right offshore company for your business? In his session at 21st Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, will discuss the things to look for - positive and negative - in evaluating your options. He will also discuss how to maximize productivity with your offshore developers. Before you start your search, clearly understand your business needs and how that impacts software choices.
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
SYS-CON Events announced today that B2Cloud will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. B2Cloud specializes in IoT devices for preventive and predictive maintenance in any kind of equipment retrieving data like Energy consumption, working time, temperature, humidity, pressure, etc.
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
While some developers care passionately about how data centers and clouds are architected, for most, it is only the end result that matters. To the majority of companies, technology exists to solve a business problem, and only delivers value when it is solving that problem. 2017 brings the mainstream adoption of containers for production workloads. In his session at 21st Cloud Expo, Ben McCormack, VP of Operations at Evernote, will discuss how data centers of the future will be managed, how th...
SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp em...
SYS-CON Events announced today that Nihon Micron will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nihon Micron Co., Ltd. strives for technological innovation to establish high-density, high-precision processing technology for providing printed circuit board and metal mount RFID tags used for communication devices. For more inf...
SYS-CON Events announced today that Suzuki Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Suzuki Inc. is a semiconductor-related business, including sales of consuming parts, parts repair, and maintenance for semiconductor manufacturing machines, etc. It is also a health care business providing experimental research for...
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...