Label change color when clicked

前端 未结 2 572
南旧
南旧 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.

    0 讨论(0)
  • 2021-01-15 19:42

    Don't use an ActiveX control for this. Any Shape can be assigned to a macro, so instead of having Click event handlers for ActiveX labels like so:

    Private Sub Label2_Click()
        'do stuff
    End Sub
    

    Make the handlers public, give them a meaningful name:

    Public Sub BuscaPorPalavraChave()
        'do stuff
    End Sub
    

    Replace the labels with TextBox shapes - make the shape fill and border transparent, right-click the shape, and select "assign macro" - then pick BuscaPorPalavraChavre. Done!

    textbox shape made to look just like a label

    Rinse & Repeat for every label. I know, painful - but worth it!

    That navigation UI looks very nice BTW =)

    0 讨论(0)
提交回复
热议问题