Hi all,
I found a strange gotcha in VFP's native BindEvent. It seems that if you bind an event handler to a property (in order to have the hanlder fire when the property changes), it will fire twice if you switch from storing an object reference in the property to a character value. The first time it fires, the value changes to NULL and the second time it changes to the value that you assigned.
I'm posting this as a reference.
-m@
Example code:
PUBLIC oform1
oform1=NEWOBJECT("form1")
oform1.Show
RETURN
DEFINE CLASS form1 AS form
Top = 0
Left = 0
Height = 220
Width = 300
LastValue = ""
Caption = "BindEvent Test"
ADD OBJECT Container1 AS container WITH ;
TOP = 20, ;
LEFT = 80, ;
HEIGHT = 60, ;
WIDTH = 120, ;
NAME = 'Container1'
ADD OBJECT Command1 AS commandbutton WITH ;
TOP = 120, ;
LEFT = 20, ;
HEIGHT = 27, ;
WIDTH = 95, ;
NAME = 'Command1', ;
CAPTION = "Blank"
ADD OBJECT command2 AS commandbutton WITH ;
AutoSize = .T., ;
Top = 120, ;
Left = 160, ;
Height = 27, ;
Width = 95, ;
Name = "Command2"
CAPTION = "Container"
PROCEDURE Init
BINDEVENT(this, "LastValue", This, "DisplayLastValueType", 1)
ENDPROC
PROCEDURE command1.Click
thisform.LastValue = ""
ENDPROC
PROCEDURE command2.Click
thisform.LastValue = ThisForm.Container1
ENDPROC
PROCEDURE DisplayLastValueType
IF VARTYPE(ThisForm.LastValue) = "O"
WAIT WINDOW TIMEOUT 2 "LastValue: Changed to Object Reference " + TRANSFORM(SECONDS())
ELSE
WAIT WINDOW TIMEOUT 2 "LastValue: Changed to blank " + NVL(ThisForm.lastValue," [Value is NULL!!!]") + " " + TRANSFORM(SECONDS())
ENDIF
ENDDEFINE