- Written by David Corrales
- Last Updated: 22 April 2016
- Created: 07 January 2014
- Hits: 12005
In the Enable and Disable Groups of Controls article we discussed how you can enable or disable group of controls by setting the parent control’s Enable property.
Now we will look at other properties that have a similar trickle down effect, but work fundamentally in a different fashion. For example, if you change the font of the Form control, you may notice that all the controls within the form change their fonts accordingly. These trickle down properties are known as ambient properties. Ambient properties are properties that if not individually set, take their value from the parent control.
There are four common ambient properties:
Cursor, Font, BackColor, ForeColor
To illustrate how ambient properties work, consider the following example:
A sample form contains two buttons and various other controls. When button1’s Font property is changed from the default, it uses the new font instead of inheriting the font of the parent form:
(Change the font of the control by using the Properties Pane)
On the other hand, button2 is using the default font value and thus inherits the parent form’s font setting:
The Properties Panel indicates a property with a non-default value by displaying its value in bold text:
Leaving the default value for the Font property on all the child controls makes it easy to change all the fonts by setting the form’s Font property to the desired font. Since the BackColor and ForeColor properties are also ambient properties, it makes it easy to theme the controls without having to alter each control’s properties individually. For example, when the form’s ForeColor property is set to Red, all the child controls follow suit (with the exception of the sample label, which has a preset ForeColor):
You can use panel and groupbox controls to set the fonts of specific groups of controls.
As you can see, ambient properties make it easier to style a large number of controls within the form without having to alter the controls individually; at the same time allowing you to customize the look of individual controls if needed.
You can download the Ambient Properties Sample here.