如果您想从字面上重用您的表单代码,您可以执行以下操作:
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 转换为布尔类型或枚举类型。