我在vba表格上有3张照片 图像是(picture1,picture2和selectedImage)
VBA将picture1或picture2设置为selectedImage。 Picture1和Picture2初始化为(none),selectedImage设置为本地位文件。
Private Sub updatePicture(position As Integer)
'This code works when un-commented out and run
'Picture1.Picture = selectedImage.Picture
'This code does not update the images
If position = 1 Then
Picture1.Picture = selectedImage.Picture
ElseIf position = 2 Then
Picture2.Picture = selectedImage.Picture
End If
End Sub
我调试并确认了position = 1,并且' Picture1.Picture = selectedImage.Picture'正在运行,但图像没有更新......
欢迎任何帮助。使用excel 2013.
答案 0 :(得分:0)
此类问题通常与Image
图片的更改不会触发事件有关。假设你肯定将1或2传递给程序(你应该检查确定你是),有几种方法可以处理重绘问题:
一种方法是在代码底部添加Repaint
方法,因此您的过程会显示为:
If position = 1 Then
Picture1.Picture = selectedImage.Picture
ElseIf position = 2 Then
Picture2.Picture = selectedImage.Picture
End If
Me.Repaint
然而,如果您要快速更新图片(例如在进度监视器或多次点击处理上)或者有一个巨大的Userform
,这将重新绘制您的整个Userform
并导致闪烁。
另一种方法是创建一个事件并通过该事件运行您的新图片。我无法从权威的位置说出这一点,但我的印象是Event
会强制重新粉饰,并且只会刷新检测到更改的Userform
部分,所以我的体验是这种方法更顺畅。
你这样做的方式非常简单。您插入一个新的Class
并适当地命名它(我称之为 clsPicHandler )。该类中的代码可能有点像:
Option Explicit
Public Event PictureChanged(img As MSForms.Image, pic As Object)
Public Sub SetPicture(img As MSForms.Image, pic As Object)
If img Is Nothing Or pic Is Nothing Then Exit Sub
If img.Picture Is pic Then Exit Sub
RaiseEvent PictureChanged(img, pic)
End Sub
然后,Userform
后面的代码会触发并处理事件,如下所示:
Option Explicit
Private WithEvents mPicHandler As clsPicHandler
Public Sub UpdatePicture(position As Integer)
If position = 1 Then
mPicHandler.SetPicture Picture1, selectedImage.Picture
ElseIf position = 2 Then
mPicHandler.SetPicture Picture2, selectedImage.Picture
End If
End Sub
Private Sub mPicHandler_PictureChanged(img As MSForms.Image, pic As Object)
img.Picture = pic
DoEvents
End Sub
Private Sub UserForm_Initialize()
Set mPicHandler = New clsPicHandler
End Sub