Properties Adopted by Child Controls


Some properties are adopted by many of the controls contained on a form. For example, by default, a Label control uses the same background color as the form. If you change the form’s BackColor property, its Label controls change to display the same color.

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 runtime, 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, so that they do not sit directly on the form.

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(ByVal sender As Object, _   ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint     Dim new_font As New Font("Comic Sans MS", 20)     e.Graphics.DrawString("Hello World!", _         new_font, Brushes.Black, 10, 10)     new_font.Dispose() 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(ByVal sender As Object, _  ByVal e As System.Windows.Forms.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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net