Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Exposing Properties of Constituent Controls

The controls that make up a user 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:

' Visual Basic
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

// C#
public Color ButtonColor
{
   get
   {
      return(myButton.BackColor);
   }
   set
   {
      myButton.BackColor = value;
   }
}

To expose a property of a constituent control

  1. In the Code Editor for your user control, create a public property.
  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

Walkthrough: Authoring a User Control With Visual Basic .NET

Show:
© 2015 Microsoft