How to: Expose Properties of Constituent Controls

The controls that make up a composite control are called constituent controls. These controls are normally declared private, and thus cannot be accessed by the developer. If you want to make properties of these controls available to future users, you must expose them to the user. A property of a constituent control is exposed by creating a property in the user control, and using the get and set accessors of that property to effect the change in the private property of the constituent control.

Consider a hypothetical user control with a constituent button named MyButton. In this example, when the user requests the ConstituentButtonBackColor property, the value stored in the BackColor property of MyButton is delivered. When the user assigns a value to this property, that value is automatically passed to the BackColor property of MyButton and the set code will execute, changing the color of MyButton.

The following example shows how to expose the BackColor property of the constituent button:

Public Property ButtonColor() as System.Drawing.Color
   Get
      Return MyButton.BackColor
   End Get
   Set(Value as System.Drawing.Color)
      MyButton.BackColor = Value
   End Set
End Property
public Color ButtonColor
{
   get
   {
      return(myButton.BackColor);
   }
   set
   {
      myButton.BackColor = value;
   }
}

To expose a property of a constituent control

  1. Create a public property for your user control.

  2. In the get section of the property, write code that retrieves the value of the property you want to expose.

  3. In the set section of the property, write code that passes the value of the property to the exposed property of the constituent control.

See also