Color Behavior Changes in Visual Basic .NET
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

Color Behavior Changes in Visual Basic .NET

Visual Studio .NET 2003

In Visual Basic 6.0, the BackColor and ForeColor properties of a control had to be explicitly set at design time or at run time; colors could not be inherited. In Visual Basic .NET, unless a color is explicitly set at design time or at run time, it will inherit the color setting of its parent.

The following example of a form with two CommandButton controls, Command1 and Command2, demonstrates the differences in behavior:

' Visual Basic 6.0 
' Command1's BackColor is left at its default (gray).
' Command2's BackColor is explicitly set.
Command2.BackColor = vbBlack
' Explicitly set the BackColor of the form.
Form1.BackColor = vbRed

After running the above code, the Command1's BackColor is still the default (gray), and Command2's BackColor is black.

The following example shows the behavior in Visual Basic .NET. In Visual Basic .NET, CommandButton controls are replaced by Button controls.

' Visual Basic .NET 
' Command1's BackColor is left at its default (gray).
' Command2's BackColor is explicitly set.
Command2.BackColor = System.Drawing.Color.Black
' Explicitly set the BackColor of the form.
Me.BackColor = System.Drawing.Color.Red

After running the above code, the Command1's BackColor is red, and Command2's BackColor is black. Because the BackColor was not explicitly set for Command1, the color is inherited from the form.

Note that this will apply for any parent, not just forms. If the Button was contained in a Panel control within the form, changing the color of either the panel or the form would change the color of the Button.

What to do next

  • Search for any code that explicitly sets the BackColor or ForeColor property at run time. If it is being set for a parent, explicitly set the color of the child controls at design time.

See Also

Control Changes in Visual Basic .NET | UserControl Changes in Visual Basic .NET

Show:
© 2016 Microsoft