Search code examples
vbaexceluserform

VBA: Can't change Image Control picture after click on element


Setup

I have an Excel VBA UserForm with an Image Control element. When clicking on buttons in the form, the Image Control's Picture source is set / updated with the following code:

Set MyForm.imgControl.Picture = LoadPicture(pathToFile)

Problem

When the user clicks on the Image Control, updating its Picture source doesn't work anymore. The problem occurs, no matter when in the workflow the Image Control is clicked:

  • Example 1: User clicks a button to set the Picture (and pictures sets correctly). User clicks on Image Control. User clicks a different button to change the Picture -> displayed picture doesn't change.
  • Example 2: User clicks on Image Control right after Form load. User clicks on button to change the Picture -> displayed picture doesn't change.

If the user never clicks on the Image Control, everything works perfectly smoothly. It's worth noting that clicking on the Image Control doesn't visibly focus it.

Question

Clicking on the Image Control shouldn't make a difference in whether or not the Picture can be updated or not. How can this be done? Or, at least, does anyone know why the explained behavior is happening, or is it just a bug?

The Control does have Click / MouseDown / ... events (which are empty), but they don't take a Cancel parameter.


Solution

  • It does sound like a bug, as you say, but as a workaround, I'd suggest forcing a repaint after changing the picture:

    Set MyForm.imgControl.Picture = LoadPicture(pathToFile)
    MyForm.Repaint