Recipe 9.6. Setting a Background Color


You want to customize a form's background color but don't want the controls on the form to look out of place.


Sample code folder: Chapter 09\BackgroundColor

No problem: most controls automatically take on the same background color as their container.


The demonstration of this effect is simple. Add the following code to a button's Click event to change the background color to some random selection. Place any controls of interest on the form to see how the changing background affects them:

 Private Sub ActBackground_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) _       Handles ActBackground.Click    ' ----- Change the background to some random color.    Dim redPart As Integer    Dim greenPart As Integer    Dim bluePart As Integer    Dim surpriseColor As New Random    redPart = surpriseColor.Next(0, 255)    greenPart = surpriseColor.Next(0, 255)    bluePart = surpriseColor.Next(0, 255)    Me.BackColor = Color.FromArgb(redPart, _       greenPart, bluePart) End Sub 

As shown in Figure 9-9, the RadioButton, Label, and CheckBox controls all adjust automatically by taking on the same background color as the containing form. The TextBox control's background remains white, by design. Place any other controls you might be using on this form to see how they behave.

Figure 9-9. Many controls automatically take on the same background color as their container

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: