Label change color when clicked

前端 未结 2 574
南旧
南旧 2021-01-15 18:53

I have a VBA/Excel that user clicks on labels (Active X - Text Label) to perform some actions. The label property is BackStyle Transparent, but when the user click, the labe

2条回答
  •  借酒劲吻你
    2021-01-15 19:34

    I came up with a different solution in case you have faced a related problem, like the transparency/opaque problem when executing a macro hovering over a transparent label but that becomes opaque when clicking on it.

    The workaround consists basically in changing the visibility status when hovering in and out. It might be counterintuitive at first (Because you are making disappear the same label you are using to execute the macro) but it works really well. Let's assume we have two Active X labels overlap. Label1 is the bigger one and label2 is the smaller one, completely contained in Label1. The code I used is like:

    Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    
    Application.ScreenUpdating = False 
    Label2.Visible = False 
    Label1.Visible = True
    ### Put your code in here
    Application.ScreenUpdating = True
    
    End Sub
    

    Then as you hover out label2, and assuming the labels are correctly overlap, you will hover over Label 1, now visible because you "activated it", and the following code is executed

    Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    
    Application.ScreenUpdating = False 
    Label2.Visible = True
    Label1.Visible = False
    ### Put your code in here
    Application.ScreenUpdating = True
    
    End Sub
    

    Please bear in mind that you need to have a figure or button in between the two Active x labels, otherwise you will get a strange result because in the exact moment you hover over Label 2, it will disappear, leaving Label 1 beneath it which will also disappear as soon as you move, makin Label 2 appear again and ... you get the idea. This will not break the excel file but will make it make strange things like blinking or delaying the mouse movement. To prevent thus. I recommend to have that shape as a safe zone.

    Hope this helps.

提交回复
热议问题