Excel

Hight Light User Form's control while mouse move on it

Ease of Use

Intermediate

Version tested with

2003,2007 

Submitted by:

mohanvijay

Description:

When the mouse moves on the control then the particular control hightlight by change back color and when mouse leaves from the control then got default back color 

Discussion:

we use userforms in all our projects.i use some times label control instead of command button control for diiferent look.but the label looks like very flat so i want to change back color and border color of the label control while mouse moving on it. 

Code:

instructions for use

			

Option Explicit 'Declare default colors Const D_Lbl_Def_Bac As Long = 10066329 Const D_Lbl_Def_Bor As Long = 5066061 Const D_Lbl_Def_FoCol As Long = 16579836 'Declare colors that apper on label while mouse moving on it Const D_Lbl_Move_Bac As Long = 13750737 Const D_Lbl_Move_Bor As Long = vbWhite Const D_Lbl_Move_FoCol As Long = 6184542 'Boolean value for each label to flag for whether the label color changed or not Dim D_Bo_Lbl_1 As Boolean 'To "Move 1" label Dim D_Bo_Lbl_2 As Boolean 'To "Move 2" Label Dim D_Bo_Lbl_3 As Boolean 'To "Move 3" Label Dim D_Bo_Lbl_4 As Boolean 'To "Move 4" Label 'Position of lable 1 Const D_L1_Top_Mi As Single = 30 Const D_L1_Top_Ma As Single = 48 Const D_L1_Left_Mi As Single = 12 Const D_L1_Left_Ma As Single = 102 'Position level of lable 2 Const D_L2_Top_Mi As Single = 30 Const D_L2_Top_Ma As Single = 48 Const D_L2_Left_Mi As Single = 126 Const D_L2_Left_Ma As Single = 216 'Position level of lable 3 Const D_L3_Top_Mi As Single = 72 Const D_L3_Top_Ma As Single = 90 Const D_L3_Left_Mi As Single = 12 Const D_L3_Left_Ma As Single = 102 'Position level of lable 4 Const D_L4_Top_Mi As Single = 72 Const D_L4_Top_Ma As Single = 90 Const D_L4_Left_Mi As Single = 126 Const D_L4_Left_Ma As Single = 216 Private Sub lbl_3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 'change the color while mouse move lbl_3.BackColor = D_Lbl_Move_Bac lbl_3.BorderColor = D_Lbl_Move_Bor lbl_3.ForeColor = D_Lbl_Move_FoCol D_Bo_Lbl_3 = True End Sub Private Sub lbl_4_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 'change the color while mouse move lbl_4.BackColor = D_Lbl_Move_Bac lbl_4.BorderColor = D_Lbl_Move_Bor lbl_4.ForeColor = D_Lbl_Move_FoCol D_Bo_Lbl_4 = True End Sub Private Sub lbl_1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 'change the color while mouse move lbl_1.BackColor = D_Lbl_Move_Bac lbl_1.BorderColor = D_Lbl_Move_Bor lbl_1.ForeColor = D_Lbl_Move_FoCol D_Bo_Lbl_1 = True End Sub Private Sub lbl_2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 'change the color while mouse move lbl_2.BackColor = D_Lbl_Move_Bac lbl_2.BorderColor = D_Lbl_Move_Bor lbl_2.ForeColor = D_Lbl_Move_FoCol D_Bo_Lbl_2 = True End Sub Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If D_Bo_Lbl_1 = True Then 'Identifies the "label 1" has changed color If Y < D_L1_Top_Mi Or Y > D_L1_Top_Ma Then 'If mouse leaves then change into defaut color lbl_1.BackColor = D_Lbl_Def_Bac lbl_1.BorderColor = D_Lbl_Def_Bor lbl_1.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_1 = False Exit Sub End If If X < D_L1_Left_Mi Or X > D_L1_Left_Ma Then 'If mouse leaves then change into defaut color lbl_1.BackColor = D_Lbl_Def_Bac lbl_1.BorderColor = D_Lbl_Def_Bor lbl_1.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_1 = False Exit Sub End If End If If D_Bo_Lbl_2 = True Then If Y < D_L2_Top_Mi Or Y > D_L2_Top_Ma Then lbl_2.BackColor = D_Lbl_Def_Bac lbl_2.BorderColor = D_Lbl_Def_Bor lbl_2.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_2 = False Exit Sub End If If X < D_L2_Left_Mi Or X > D_L2_Left_Ma Then lbl_2.BackColor = D_Lbl_Def_Bac lbl_2.BorderColor = D_Lbl_Def_Bor lbl_2.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_2 = False Exit Sub End If End If If D_Bo_Lbl_3 = True Then If Y < D_L3_Top_Mi Or Y > D_L3_Top_Ma Then lbl_3.BackColor = D_Lbl_Def_Bac lbl_3.BorderColor = D_Lbl_Def_Bor lbl_3.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_3 = False Exit Sub End If If X < D_L3_Left_Mi Or X > D_L3_Left_Ma Then lbl_3.BackColor = D_Lbl_Def_Bac lbl_3.BorderColor = D_Lbl_Def_Bor lbl_3.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_3 = False Exit Sub End If End If If D_Bo_Lbl_4 = True Then If Y < D_L4_Top_Mi Or Y > D_L4_Top_Ma Then lbl_4.BackColor = D_Lbl_Def_Bac lbl_4.BorderColor = D_Lbl_Def_Bor lbl_4.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_3 = False Exit Sub End If If X < D_L4_Left_Mi Or X > D_L4_Left_Ma Then lbl_4.BackColor = D_Lbl_Def_Bac lbl_4.BorderColor = D_Lbl_Def_Bor lbl_4.ForeColor = D_Lbl_Def_FoCol D_Bo_Lbl_4 = False Exit Sub End If End If End Sub

How to use:

  1. Open the attached file
  2. Click Show Form button the userform will show
  3. move mouse on the labels
 

Test the code:

  1. Open the attached file
  2. Click Show Form button the userform will show
  3. move mouse on the labels
 

Sample File:

Mouse Move in User form.zip 14KB 

Approved by Jacob Hilderbrand


This entry has been viewed 27 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express