February 28, 1996
This article explains how to add visual appeal to your Microsoft® Visual Basic® version 4.0 application by changing the color of a Label control when the mouse is positioned over that control.
When designing a Microsoft® Visual Basic® version 4.0 application, you can change the color of a Label control when the mouse pointer is positioned over that control. Then, when the mouse pointer is moved away from the control, the original ForeColor property is restored. This technique lets you draw attention temporarily to a specific Label control while your application is running.
Because a Label control does not have an hWnd property, you cannot use Microsoft Windows® application programming interface (API) functions to determine whether the mouse pointer is hovering over the control. You can, however, monitor the MouseMove event of the Label control to determine when you should change the ForeColor property of the Label control.
This program shows how to change the color of a Label control when the mouse pointer is positioned over the control.
Dim MyFocusColor, MyNormalColor
Dim Lbl As Label
Private Sub Form_Load()
MyNormalColor = QBColor(0)
MyFocusColor = QBColor(10)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
For Each i In Me.Controls
If TypeOf i Is Label Then
If i.ForeColor <> MyNormalColor Then
i.ForeColor = MyNormalColor
End If
End If
Next i
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
Call ChangeColor(Label1)
End Sub
Private Sub Label2_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
Call ChangeColor(Label2)
End Sub
Sub ChangeColor(Lbl As Label)
If Lbl.ForeColor <> MyFocusColor Then
Lbl.ForeColor = MyFocusColor
End If
End Sub
Run the example program by pressing F5. Each time you move the mouse pointer over the Label control, the color of the Label control is changed.
"BackColor, ForeColor Properties." (MSDN Library, Developer Products, Visual Basic 5.0 Documentation)