Common properties are properties that are shared by most objects in the VB6 Toolbox. Some have more, while others have fewer. In this section we’ll start looking at four of them.
The BackColor and ForeColor properties
The BackColor property sets the background color of an object while the ForeColor property changes the foreground color used to display text.
You can set these properties either from Properties Window or you may wish to set in run-time.
Example
When you click on command1 button, the code in the Click event procedure is executed.
Private Sub cmdChangeColor_Click() Label1.BackColor = vbRed Label1.ForeColor = vbBlue End Sub
On execution, the background color of the label will be red and labels text color will be blue.
vbRed and vbBlue are the color constants.
Here is another example :
Private Sub cmdChangeColor_Click() Label1.BackColor = vbRed Label1.ForeColor = vbBlue Form1.BackColor = vbGreen Text1.BackColor = vbBlack Text1.ForeColor = vbYellow Frame1.BackColor = vbWhite End Sub
The color can also be expressed in hexadecimal code.
Example
Private Sub cmdChangeColor_Click() Label1.BackColor = &HFF& Label1.ForeColor = &HC00000 End Sub
In this case, you have to copy the hexadecimal code from Properties Window.
The Font Property
You can set the font properties from the Properties Window. See the below example to set property in run time.
Example
Private Sub cmdChangeFont_Click() Text1.FontSize = 16 Text1.FontBold = True Text1.FontItalic = True Text1.Font = "Tahoma" End Sub
The above block of code can be written in the following way as well:
Private Sub cmdChangeFont_Click() Text1.Font.Size = 16 Text1.Font.Bold = True Text1.Font.Italic = True Text1.Font.Name = "Tahoma" End Sub
QuickHint:
Don’t forget to turn intellisense on in the IDE, if you haven’t already. Intellisense gives you the ability to type the first one or two characters of a property, then pop up a list for you to finish it from.
The Caption property
It sets the text displayed in the objects title bar or on the object.
Example
Private Sub cmdSetTitle_Click() Form1.Caption = "New Program" Label1.Caption = "Hello" Frame1.Caption = "New frame" End Sub
The Caption property of form sets the forms title text. The text to be displayed on label is set.
The Text property
It sets the text in a TextBox.
Example
Text1.Text = "New program"
Here the text string is assigned to Text1.Text
That’s it for the first section of common properties. In the next section, we’ll cover a nine more:
- Left
- Top
- Width
- Height
- Container
- Visible
- Enabled
- MousePointer
- MouseIcon