PROPERTIES ADOPTED BY CHILD CONTROLS

Some properties are adopted by many of the child controls contained in a parent control or in a form. For example, by default, a Label control uses the same background color as the form that contains it. If you change the form’s BackColor property, its Label controls change to display the same color. Similarly if a GroupBox contains a Label and you change the GroupBox’s BackColor property, its Label changes to match.

Some properties adopted by a form’s controls include BackColor, ContextMenu, Cursor, Enabled, Font, and ForeColor. Not all controls use all of these properties, however. For example, a TextBox only matches its form’s Enabled and Font properties.

If you explicitly set one of these properties for a control, its value takes precedence over the form’s settings. For example, if you set a Label control’s BackColor property to red, the control keeps its red background even if you change the Form’s BackColor property.

Some of these properties are also not tremendously useful to the Form object itself, but they give guidance to the form’s controls. For example, a form doesn’t automatically display text on its surface, so it never really uses its Font property. Its Label, TextBox, ComboBox, List, RadioButton, CheckBox, and many other controls adopt the value of this property, however, so the form’s Font property serves as a central location to define the font for all of these controls. If you change the form’s Font property, even at run time, all of the form’s controls change to match. The change applies to all of the form’s controls, even those contained within GroupBoxes, Panels, and other container controls.

These properties can also help your application remain consistent both with the controls on the form and with other parts of the application. For example, the following code draws the string “Hello World!” on the form whenever the form needs to be repainted. This code explicitly creates the Comic Sans MS font.

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    Using new_font As New Font("Comic Sans MS", 20)
        e.Graphics.DrawString("Hello World!", 
            new_font, Brushes.Black, 10, 10)
    End Using
End Sub

Rather than making different parts of the program build their own fonts, you can use the forms’ Font properties as shown in the following code. This makes the code simpler and ensures that different pieces of code use the same font.

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    e.Graphics.DrawString("Hello World!", Me.Font, Brushes.Black, 10, 100)
End Sub

As a nice bonus, changing the form’s Font property raises a Paint event, so, if the form’s font changes, this code automatically runs again and redraws the text using the new font.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.117.207