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
Create a public property for your user control.
In the get section of the property, write code that retrieves the value of the property you want to expose.
In the set section of the property, write code that passes the value of the property to the exposed property of the constituent control.
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Learn how to implement read-write, read-only, and write-only class properties using property accessors and access modifiers, and how to implement methods and extension methods for a class.