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

Test.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 に答える