Button hover shadow

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bright1Light
    New Member
    • Mar 2023
    • 5

    Button hover shadow

    I have button
    I want to add shadow only when hover
    How I do it?
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    #2
    In simple terms you would have the shadow turned off by design and then turn it on & off within the Event procedure for the form's Move event, depending on the latest position of the pointer as reported.

    Unfortunately, when I looked in my 2019 version, I found no way to turn shadowing on or off :-(

    I also had difficulties making sense of the Form_MouseMove documentation on Microsoft's web site. I left a comment to that effect but couldn't get it to work even to change the size of the font :-(

    However, I will keep this flagged and attempt to find out more for you. Watch this space.

    In the meantime, the basic logic should be something like the following which I knocked up to test :
    Code:
    Option Compare Database
    Option Explicit
    
    Private Const conCB As String = "cmdTest"
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer _
                             , X As Single, Y As Single)
        Dim lngFS As Long
    
        With Me
            With .Controls(conCB)
                If X < .Left Or X > .Left + .Width _
                Or Y < .Top Or Y > .Top + .Height Then
                    lngFS = 11
                Else
                    lngFS = 15
                End If
                If .FontSize <> lngFS Then .FontSize = lngFS
            End With
        End With
    End Sub
    You can ask questions about this once you've read & tried to understand it.

    Here's the basic design of the form. Very simple with a Button control in the middle of an otherwise empty form with a 1cm gap on all sides.
    [IMGNOTHUMB]https://bytes.com/attachment.php? attachmentid=10 594&stc=1&d=167 8641537[/IMGNOTHUMB]
    Attached Files
    Last edited by NeoPa; Mar 12 '23, 05:19 PM.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32668

      #3
      I now learn, from a few of the other Access MVPs who generously chipped in to help me understand this better, that each object (Section, Command Button, etc.) needs to be handled by its own OnMouseMove event procedure.

      I also learned that there is a .Shadow property - but which wasn't available in my version A2019 :-( CommandButton.S hadow property (Access) explains how it needs to be set for the various different possible options. In my code below I'll use the value 1 to set it and 0 to clear it.
      Code:
      Option Compare Database
      Option Explicit
      
      Private Const conCB As String = "cmdTest"
      
      Private Sub Detail_MouseMove(Button As Integer, Shift As Integer _
                                 , X As Single, Y As Single)
          With Me.Controls(conCB)
              If .Shadow <> 0 Then .Shadow = 0
          End With
      End Sub
      
      Private Sub cmdTest_MouseMove(Button As Integer, Shift As Integer _
                                  , X As Single, Y As Single)
          With Me.Controls(conCB)
              If .Shadow <> 1 Then .Shadow = 1
          End With
      End Sub
      Note that, even though one of the event procedures is set specifically for the Command Button object itself, the code still needs to use the name of the object in order to reference it when setting it's Shadow property - hence line #4 where it's set as a constant.
      Last edited by NeoPa; Mar 13 '23, 12:38 AM. Reason: Corrected reference to .Shadow property.

      Comment

      • isladogs
        Recognized Expert Moderator Contributor
        • Jul 2007
        • 483

        #4
        Sorry to disagree but the Shadow property has in fact been available since Access 2010 when Quick Styles were introduced.
        However, like the rest of the Quick Styles items, it isn't shown in the button's control sheet

        The syntax is either CommandButtonNa me.Properties(" Shadow") or just CommandButtonNa me.Shadow

        So for example, if you have a button called Command0, you can use code like this on the button's mouse move event:

        Code:
        Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
            Me.Command0.BorderColor = vbRed 'add a red border
            Me.Command0.Properties("Shadow") = 1 'add a drop shadow at bottom right (or try 18 or 22)
        End Sub
        You will then need to remove that code when you move away from the button so use code like this in the form's Detail_MouseMov e event:

        Code:
        Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
            Me.Command0.BorderColor = RGB(132, 161, 198) 'restore blue border
            Me.Command0.Properties("Shadow") = 0 'remove shadow
        End Sub
        I have attached a simple example demonstrating the use of the above code.

        Also, see the Microsoft Help article: CommandButton.S hadow property
        Attached Files

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32668

          #5
          Hi isladogs.

          Before I continue I should add that he was indeed one of the other Access MVPs who steered me in the right direction on the .Shadow property earlier. Thanks for that again :-)

          I'm happy for you to disagree on when it was introduced. I'd rather be shown up than steer anyone incorrectly. I'd made an assumption as to the cause of my not seeing it but that appears incorrect. I tried to check but the documentation had nothing on it I could find.

          As for the use of the .Shadow property, I have since updated the code to show what I'd meant before. The text explains the situation correctly but I'd forgotten to change the code from my own testing and I'd used different font sizes to test the concept worked before posting. As you can see I used the simpler second alternative that you show rather than using the .Properties() collection.

          Comment

          • isladogs
            Recognized Expert Moderator Contributor
            • Jul 2007
            • 483

            #6
            Hi NeoPa
            That's better! Your corrected code does indeed work.

            For the benefit of the OP, using the simplest version of the code as in my post #4 will cause screen flicker as the code will repeatedly update the appearance every time the mouse moves. To prevent that, add code (as @NeoPa did in post #3) to first check the current state of the button so the changes only occur once. My variation on that idea is:

            Code:
            Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
            
                'check current state of control so it only changes once
                If Not Me.Command0.BorderColor = vbRed Then Me.Command0.BorderColor = vbRed 'add a red border
                
                If Not Me.Command0.Shadow = 1 Then Me.Command0.Shadow = 1 'add a drop shadow at bottom right (or try 18 / 22 etc)
            End Sub
            
            Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
            
               'check current state of control so it only changes once
                If Not Me.Command0.BorderColor = RGB(132, 161, 198) Then Me.Command0.BorderColor = RGB(132, 161, 198) 'restore blue border
                
                If Not Me.Command0.Shadow = 0 Then Me.Command0.Shadow = 0 'remove shadow
            End Sub
            Last edited by NeoPa; Mar 13 '23, 08:54 PM. Reason: Simply removed white space at end of code.

            Comment

            Working...