0

以下代码是更改图像或通知用户图像是否已更改的表单代码。在表单中,我有一个名为Image1的 Image 控件,它的 Picture 属性必须更改。我只是寻求有关如何从此代码制作类模块(.cls)的帮助。

Private Image1Color As String

Private Sub Form_Load()

    Image1Color = "Green"

End Sub

Private Sub CheckIn1_Click()

    If Image1Color = "Green" Then
        Image1.Picture = LoadPicture ("Color\red1.jpg")
        Image1Color = "Red"
    Else
        MsgBox ("This table is already occupied")
    End If
End Sub
4

1 回答 1

0

如果您想从字面上重用您的表单代码,您可以执行以下操作:

ImageControlWrapper.cls:

Private m_ksColor_Green As String = "Green"
Private m_ksColor_Red As String = "Red"

Private m_sImageColor As String
Private WithEvents m_oImageControl As Image

Private Sub Class_Initialize()

    m_sImageColor = m_ksColor_Green

End Sub

Public Sub Attach(ByRef in_oImageControl As VB.Image)

    Set m_oImageControl = in_oImageControl

End Sub

Private Sub m_oImageControl_Click()

    If m_sImageColor = m_ksColor_Green Then
        Set m_oImageControl.Picture = LoadPicture("Color\red1.jpg")
        m_sImageColor = m_ksColor_Red
    Else
        MsgBox "This table is already occupied"
    End If

End Sub

测试.frm:

Private m_oImageControlWrapper As ImageControlWrapper

Private Sub Form_Load

    Set m_oImageControlWrapper = New ImageControlWrapper
    m_oImageControlWrapper.Attach Image1

End Sub

我纯粹使用字符串常量来表示颜色,以便编译器可以在您拼错常量时发现错误。如果你拼错了实际的字符串,这是一个需要修复的烦人的错误。但是,如果您真的不需要使用字符串,最好将 m_sImageColor 转换为布尔类型或枚举类型。

于 2012-07-19T00:01:03.797 回答