Export (0) Print
Expand All

Walkthrough: Adding a Simple Attribute to a User Control 

This walkthrough demonstrates adding 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 is similar to that of the desktop, except that device projects store this information in a separate metadata file (.xmta).

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. 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#, expand Smart Device, and then click Pocket PC 2003.

  3. In the Templates pane, click Control Library.

  4. In the Name box, type MyControlLibrary, 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 on the shortcut menu, click View Class Diagram.

    A rounded rectangle representing the class diagram opens.

  2. Right-click the class diagram, and then on the shortcut menu, 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 on the shortcut menu, 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 on the shortcut menu, 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, on the shortcut menu, point to Add, and then click New Item.

  2. In the Add New Item dialog box, select Windows Form, 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 on the shortcut menu, 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.

Community Additions

ADD
Show:
© 2014 Microsoft