Walkthrough: Adding a Simple Attribute to a User Control
This walkthrough demonstrates how to add an attribute to a user control in a device project. Specifically, you add a custom attribute that makes a property of the control invisible at design time. You might want to add this feature to a project to prevent a property value from being changed.
The process resembles the desktop process except that device projects store this information in a separate metadata file (.xmta).
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
This walkthrough was written using Visual C# Development Settings.
To create the UserControl1 class
On the File menu, point to New, and then click Project.
In the Project types pane, expand Visual C#, and then click Smart Device.
In the Templates pane, click Smart Device Project.
In the Name box, type MyControlLibrary, and then click OK.
In the Add New Smart Device Project dialog box, select the Pocket PC 2003 from the Target platform drop-down, select .NET Compact Framework Version 2.0 from the .NET Compact Framework drop-down, click Control Library in the Templates pane, and then click OK.
The designer opens with a square representing the new user control class.
To add a property
In Solution Explorer, right-click UserControl1.cs, and then click View Class Diagram.
A rounded rectangle that represents the class diagram opens.
Right-click the class diagram, and then click Class Details.
In the Properties section of the Class Details window, at the <add property> prompt, type MyProperty.
In the Type column, replace int with string.
Right-click the icon at the beginning of the MyProperty row, and then click Properties.
To specify a value for the Custom Attributes property, click the ellipsis button (…) to open the Custom Attributes dialog box.
Type Browsable(false), and then click OK.
Solution Explorer displays a design-time attribute .xmta file (DesignTimeAttributes.xmta) that contains the custom attribute.
To build the control library
In Solution Explorer, right-click UserControl1.cs, and then click View Code.
Comment out the line that throws the System.NotImplementedException, and insert return ""; instead as the get action.
On the Build menu, click Build MyControlLibrary.
To test that MyProperty does not appear in the Properties browser
In Solution Explorer, right-click MyControlLibrary, point to Add, and then click New Item.
In the Add New Item dialog box, click Windows Forms in the Categories pane, and click Windows Form in the Templates pane, and then click Add.
From the Toolbox, drag UserControl1 onto the form.
Right-click the user control image on the form, and then click Properties.
MyProperty does not appear in the Property browser.
In Solution Explorer, double-click the .xmta file, and then replace false with true.
Repeat the steps to view the Properties grid. Note that MyProperty now appears.