Udostępnij za pośrednictwem


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

Note

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

  1. On the File menu, point to New, and then click Project.

  2. In the Project types pane, expand Visual C#, and then click Smart Device.

  3. In the Templates pane, click Smart Device Project.

  4. In the Name box, type MyControlLibrary, and then click OK.

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

  1. In Solution Explorer, right-click UserControl1.cs, and then click View Class Diagram.

    A rounded rectangle that represents the class diagram opens.

  2. Right-click the class diagram, and then click Class Details.

  3. In the Properties section of the Class Details window, at the <add property> prompt, type MyProperty.

  4. In the Type column, replace int with string.

  5. Right-click the icon at the beginning of the MyProperty row, and then click Properties.

  6. To specify a value for the Custom Attributes property, click the ellipsis button (…) to open the Custom Attributes dialog box.

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

  1. In Solution Explorer, right-click UserControl1.cs, and then click View Code.

  2. Comment out the line that throws the System.NotImplementedException, and insert return ""; instead as the get action.

  3. On the Build menu, click Build MyControlLibrary.

To test that MyProperty does not appear in the Properties browser

  1. In Solution Explorer, right-click MyControlLibrary, point to Add, and then click New Item.

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

  3. From the Toolbox, drag UserControl1 onto the form.

  4. Right-click the user control image on the form, and then click Properties.

    MyProperty does not appear in the Property browser.

  5. In Solution Explorer, double-click the .xmta file, and then replace false with true.

  6. Repeat the steps to view the Properties grid. Note that MyProperty now appears.

See Also

Reference

Visual Basic and Visual C# (How Do I in Smart Devices)