我在VBA表格上有3个图片。
图像为(图1,图片2和选定图像)
VBA设置Picture1或Picture2等于选定图像。Picture1和Picture2初始化为(无),然后将SelectedImageis设置为本地Bitfile。
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
我调试并确认位置= 1,并且正在运行'picture1.picture = selectimage.picture'
欢迎任何帮助。使用Excel2013。
这种类型的问题通常与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
并适当地命名(我称为Mine 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