Bagikan melalui


Color Behavior for Visual Basic 6.0 Users

The ForeColor and BackColor properties of controls behave differently in Visual Basic 6.0 compared to Visual Basic 2008.

Conceptual Differences

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 and colors could not be inherited. In Visual Basic 2008, unless a color is explicitly set at design time or at run time, it inherits the color setting of its parent.

Code Changes for Color Behavior

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, Command1's BackColor is still the default (gray), and Command2's BackColor is black.

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

' Visual Basic' 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, Command1's BackColor is red and Command2's BackColor is black. Because the Command1's BackColor was not explicitly set, red is inherited from the form.

Note

   This applies for any parent, not just for 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.

Upgrade Notes

When a Visual Basic 6.0 application is upgraded to Visual Basic 2008, the upgrade wizard has no way to determine if colors will be inherited. After upgrading, search for any code that explicitly sets the BackColor or ForeColor property at run time. If it is set for a parent, explicitly set the color of the child controls at design time.

See Also

Reference

Color Handling for Visual Basic 6.0 Users

Other Resources

Windows Forms Controls for Visual Basic 6.0 Users