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

。_饼干妹妹 提交于 2020-06-27 15:41:13

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/47267087/vba-cant-change-image-control-picture-after-click-on-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!