472,119 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Button hover shadow

3 2Bits
I have button
I want to add shadow only when hover
How I do it?
1 Week Ago #1

✓ answered by isladogs

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 CommandButtonName.Properties("Shadow") or just CommandButtonName.Shadow

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.     Me.Command0.BorderColor = vbRed 'add a red border
  3.     Me.Command0.Properties("Shadow") = 1 'add a drop shadow at bottom right (or try 18 or 22)
  4. End Sub
  5.  
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_MouseMove event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.     Me.Command0.BorderColor = RGB(132, 161, 198) 'restore blue border
  3.     Me.Command0.Properties("Shadow") = 0 'remove shadow
  4. End Sub
  5.  
I have attached a simple example demonstrating the use of the above code.

Also, see the Microsoft Help article: CommandButton.Shadow property

5 5441
NeoPa
32,497 Expert Mod 16PB
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 :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Const conCB As String = "cmdTest"
  5.  
  6. Private Sub Form_MouseMove(Button As Integer, Shift As Integer _
  7.                          , X As Single, Y As Single)
  8.     Dim lngFS As Long
  9.  
  10.     With Me
  11.         With .Controls(conCB)
  12.             If X < .Left Or X > .Left + .Width _
  13.             Or Y < .Top Or Y > .Top + .Height Then
  14.                 lngFS = 11
  15.             Else
  16.                 lngFS = 15
  17.             End If
  18.             If .FontSize <> lngFS Then .FontSize = lngFS
  19.         End With
  20.     End With
  21. 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.

Attached Images
File Type: jpg Bytes.Jpg (34.5 KB, 163 views)
1 Week Ago #2
NeoPa
32,497 Expert Mod 16PB
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.Shadow 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.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Const conCB As String = "cmdTest"
  5.  
  6. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer _
  7.                            , X As Single, Y As Single)
  8.     With Me.Controls(conCB)
  9.         If .Shadow <> 0 Then .Shadow = 0
  10.     End With
  11. End Sub
  12.  
  13. Private Sub cmdTest_MouseMove(Button As Integer, Shift As Integer _
  14.                             , X As Single, Y As Single)
  15.     With Me.Controls(conCB)
  16.         If .Shadow <> 1 Then .Shadow = 1
  17.     End With
  18. 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.
1 Week Ago #3
isladogs
409 Expert Mod 256MB
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 CommandButtonName.Properties("Shadow") or just CommandButtonName.Shadow

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.     Me.Command0.BorderColor = vbRed 'add a red border
  3.     Me.Command0.Properties("Shadow") = 1 'add a drop shadow at bottom right (or try 18 or 22)
  4. End Sub
  5.  
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_MouseMove event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.     Me.Command0.BorderColor = RGB(132, 161, 198) 'restore blue border
  3.     Me.Command0.Properties("Shadow") = 0 'remove shadow
  4. End Sub
  5.  
I have attached a simple example demonstrating the use of the above code.

Also, see the Microsoft Help article: CommandButton.Shadow property
Attached Files
File Type: zip ButtonHover.zip (19.7 KB, 5 views)
1 Week Ago #4
NeoPa
32,497 Expert Mod 16PB
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.
1 Week Ago #5
isladogs
409 Expert Mod 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.  
  3.     'check current state of control so it only changes once
  4.     If Not Me.Command0.BorderColor = vbRed Then Me.Command0.BorderColor = vbRed 'add a red border
  5.  
  6.     If Not Me.Command0.Shadow = 1 Then Me.Command0.Shadow = 1 'add a drop shadow at bottom right (or try 18 / 22 etc)
  7. End Sub
  8.  
  9. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  10.  
  11.    'check current state of control so it only changes once
  12.     If Not Me.Command0.BorderColor = RGB(132, 161, 198) Then Me.Command0.BorderColor = RGB(132, 161, 198) 'restore blue border
  13.  
  14.     If Not Me.Command0.Shadow = 0 Then Me.Command0.Shadow = 0 'remove shadow
  15. End Sub
1 Week Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by bart plessers | last post: by
reply views Thread by bart plessers | last post: by
5 posts views Thread by JohnSouth | last post: by
3 posts views Thread by jack | last post: by
reply views Thread by phil-news-nospam | last post: by
4 posts views Thread by phil-news-nospam | last post: by
4 posts views Thread by tomb | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.