如何清除用户窗体中的图像?

How to clear an Image in a Userform?

我有一个使用图像控件的用户窗体。我根据两个条件在此控件中显示图片。

当我想输入两个新条件时,我不知道如何清除用户窗体中的这张图片。

您可以通过 Image1.Picture = LoadPicture(vbNullString) 轻松 'clear' 您的用户窗体图像控件(例如 Image1)。在下面的示例中,您将在单击命令按钮时在显示已定义图片和显示空控件之间切换。为了控制图像状态(是否为空),您通过 If Image1.Picture Is Nothing Then...

检查图像图片 属性

用户窗体模块中的代码示例 (Excel)

Option Explicit

Private Sub CommandButton1_Click()
' Purpose: Change view between given image and no image
Dim sImgName As String                     ' picture name string
sImgName = "C:\Temp\MyPicture.gif""        ' <<< choose your picture name"
With Me.Image1
  If .Picture Is Nothing Then             ' picture property has been cleared already
     .Picture = LoadPicture(sImgName)
  Else                                    ' a picture is already displayed
     .Picture = LoadPicture(vbNullString) ' clear it now
  End If
End With
End Sub

在 MS Access 中使用的提示

注意在 MS Access 中重复@DrMarbuse 的评论作为后期编辑的不同用法:

In Access 2010, the picture is directly set by the image path. So LoadPicture() shall not be used anymore (In Excel it is required). Everything stayed the same and worked like a charm.