I want to handle the click on an CommandButton from within a class using the following class code
Option Explicit
Private m_First As MSForms.CommandButton
Private WithEvents evFirst As MSForms.CommandButton
Property Get First() As MSForms.CommandButton
Set First = m_First
End Property
Property Let First(ByRef o As MSForms.CommandButton)
Set m_First = o
Set evFirst = o
End Property
Private Sub evFirst_Click()
MsgBox "It Worked!"
End Sub
In addition to it not working, am wondering why the reference for the Button in the form is different from that in the class, ie:
Sub Tester()
Dim f As New UserForm1
Dim o As New cButtonClass
o.First = f.CommandButton1
Dim k1 As LongLong: k1 = ObjPtr(o.First)
Dim k2 As LongLong: k2 = ObjPtr(f.CommandButton1)
Debug.Assert k1 = k2 'NOPE!
End Sub
Why doesn't this work? What is the fix?
Property LettoProperty Set. Second, in the form code you need to saySet o.First. There may be other issues, too.evFirstobject is identical to them_Firstobject. TheWithEventskeyword simply allows you to write custom code for object you declaredWithEventsfor. Essentially, you don't need thatm_Firstvariable....ObjPtris not reliable after it has been wrapped in aVariant. You need to useVarPtrin that case.Debug.Assert VarPtr(o.First) = VarPtr(f.CommandButton1)should pass.