以下代码是表单代码,用于更改图像或通知用户图像是否已更改。在表单中,我有一个名为 Image1 的图像控件,其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
答案 0 :(得分: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
Test.frm:
Private m_oImageControlWrapper As ImageControlWrapper
Private Sub Form_Load
Set m_oImageControlWrapper = New ImageControlWrapper
m_oImageControlWrapper.Attach Image1
End Sub
我纯粹使用字符串常量作为颜色,以便编译器可以在错误拼写常量时发现错误。如果你拼写错误的实际字符串,这是一个令人讨厌的bug。但是,如果您真的不需要使用该字符串,最好将m_sImageColor转换为布尔值或枚举类型。