Picture on CommandButton shows as grey rectangle when button is disabled

VisualB

New Member
Joined
Nov 14, 2023
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
On my Userform, several CommandButtons have pictures instead of text, using
VBA Code:
Me.btnSave.Caption = ""
Me.btnSave.Picture = LoadPicture(btnDir & "Save.gif")

Which yields a userform with colorful icons:
MrExcelCommandButtonWithPictures.png


When disabling the CommandButtons, using
VBA Code:
Me.btnSave.Enabled = False

the buttons become disabled as expected but the pictures on the CommandButtons just become greyed out rectangles:
MrExcelCommandButtonWithPictures_Disabled.png


Whenever a button is disabled:
• Is there any way to have the existing image show as usual, but in greyscale?
• Are there other ways to not have a solid grey rectangle appear in its place?

I tried loading another Picture in its place whenever the button gets disabled, but it would also only show as a grey rectangle

Much thanks in advance. (P.S. This is my first post - if I posted in the wrong category, please move to correct category)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Interesting. Doesn't happen for me. Button4 back style is 1; BackColor is &H8000000F& ; there is one space character in the caption; enabled property is True in the property sheet. When I click another button and set the enabled property of button4 to false, the picture remains and the button does not respond to mouse click as expected.
Office 365, Excel version 2205, build 16.0
 
Upvote 0
Thank you Micron! Based on your answer I tried the following:

Changing caption from empty "" to single space " "
Changing backstyle to 0 - fmBackStyleTransparent
Changing backstyle to 1 - fmBackStyleOpaque
Trying different Backcolor values, including the default &H8000000F&

All of those attempts still yielded a greyed out rectangle instead of the image whenever the CommandButton.Enabled property is set to False

So as a work-around this gave satisfactory results:
Instead of disabling the button, setting its Locked-property to True and loading a grayscale image instead.
VBA Code:
'Me.btnSave.Enabled = False
If Not Me.btSave.Locked Then
    Me.btSave.Locked = True
    Me.btSave.TabStop = False
    Me.btSave.Picture = LoadPicture(btnDir & "Save_grayscale.gif")
Else
    Me.btSave.Locked = False
    Me.btSave.TabStop = True
    Me.btSave.Picture = LoadPicture(btnDir & "Save.gif")
End If

By setting Tabstop=False and Locked=True the CommandButton appears to behave exactly the same as if Enabled was set to False, except that the picture does not turn into a gray rectangle.

Not sure if this next question should go into a separate thread, but is there a way to change a Picture that was previously loaded onto a CommandButton to grayscale and increasing its brightness and contrast using VBA?
I've been searching extensively on this but so far did not come across solutions that apply to the Picture property of CommandButtons.
 
Upvote 0
A picture is a file and the control is just a container for that file. It has no ability to modify anything the file (such as opacity). AFAIK the only thing you can do is load different images into it.

Maybe if the image had transition effects (think of gradient effects) and was wider than the image control, you could choose to show portions of the image (left-most vs all for example) by varying the control width. However, the control would need to be able to allow the image to be full size in it. In Access, this is controlled by the picture mode properties (clip, stretch or zoom). If clip, you only see as much of the image as the size of the control allows, i.e. the image does not resize to fit but I have no idea if that would allow you to show the left vs middle vs right side portions of an image. I doubt it.

Don't know if Excel image control provides the same properties. The same properties don't seem to be listed for Excel like they are for Access but that doesn't always mean you can't set a property in code that you can't find in the property sheet, but you'd think the documentation would cover it if it exists for the M$ application.

I think you would have to swap images out to achieve this.
 
Upvote 0
Appreciate your detailed answer, Micron!
Can one load a picture into a temporary variable (or object), modify that variable and then assign it to the CommandButton.Picture property?
Something similar to the (non-working) code below?
VBA Code:
Dim pic As Picture
Set pic = LoadPicture(ThisWorkbook.Path & "\buttons\save.gif")
pic.PictureFormat.IncrementBrightness 0.5
pic.PictureFormat.IncrementContrast 0.25
pic.PictureFormat.Grayscale
CommandButton1.Picture = pic
If that's not an option, I will just go with swapping images from the harddrive using separate calls to LoadPicture()
 
Upvote 0
I don't know, you would have to try it. Apparently you can do this with shapes, but you're using images? So I doubt it but have never tried. What's interesting is that I get a different result than you. Maybe it's a version thing.
 
Upvote 0
Office Professional 2021, Excel version 2308 (Build 16731.20234 Click-to-Run)

It does not seem possible to convert shapes to images to then assign to the CommandButton.Picture property

The LoadPicture function has the arguments [filename], [widthDesired], [heightDesired] and [flags] where [flags] can be any of the following: Color, Default, Monochrome or VgaColor
Besides the filename, none of the additional arguments seem to have any effect whatsoever, thus icons/pictures need to have the desired dimensions and appearance before calling LoadPicture()

MrExcelCommandButtonWithPictures_LoadPictureArgs_flags.png


Thanks for your help Micron. I managed to find a work-around solution to my problem by loading colorful icons into one collection, and greyscale icons into another (which were manually created without VBA code). And then just loading the corresponding icons onto the buttons when they should appear disabled, along with setting .Locked=True and .TabStop=False but keeping .Enabled=True at all times.

MrExcelCommandButtonWithPictures_IconsEnabled.png
majority of icons: .Locked = False and .Tabstop = True
MrExcelCommandButtonWithPictures_IconsDisabled.png
majority of icons: .Locked = True and .Tabstop =False

MrExcelCommandButtonWithPictures_IconFiles.png
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top