mak101
mak101

Reputation: 147

visual Foxpro releasing form without executing remaining code

I have an old application built in Visual Foxpro 9. I want to terminate the form without executing remaining code. Form.Release event release the form after executing remaining code in event it called from. I want to use this when any error occurred, can anyone please help?

I may have other forms open and dont want to close other forms in event of error on one form. Cancel would close all open forms, so I cant use it.

Upvotes: 0

Views: 2382

Answers (3)

nmatta16
nmatta16

Reputation: 1

On the Form's Error event place the following code:


LPARAMETERS nError, cMethod, nLine

*Handle the error/ clean up/ MessageBox / Log error to file or table / etc.

thisform.release

RETURN TO MASTER


** Hope this helps

Upvotes: 0

Cetin Basoz
Cetin Basoz

Reputation: 23837

Main.prg

Do Form form1
Do Form form2
Do form formN
Read Events

Errhandle.prg

lparameters tnerror, tcMessage, tcProg, tnLineNo
Local lcError
TEXT to m.lcError textmerge noshow
Error No: << m.tnerror >>. Message: << m.tcMessage >>
Source: << m.tcProg >> at line << m.tnLineNo >>
ENDTEXT
Messagebox(m.lcError, 0+4096, "Error", 5000)

Form method where error is expected:

Local loForm
loForm = thisform
On Error ;
  return ErrHandle(Error( ), Message( ), Program( ), Lineno( )) ;
         and m.loForm.Release()
local lc
lc = "Hello"
If m.lc = 1 && explicit error


EndIf

_screen.Caption = "This code wouldn't run"
On Error

Note: This is sort of using try ... catch blocks. I personally don't use try ... catch because unlike on error it doesn't catch all errors.

Upvotes: 0

Cetin Basoz
Cetin Basoz

Reputation: 23837

Main.Prg

On Error Do ErrHandle With Error( ), Message( ), Program( ), Lineno( )

Do Form form1
Do Form form2
Do form formN
Read Events


Procedure ErrHandle(tnerror, tcMessage, tcProg, tnLineNo)
    Local lcError
    TEXT to m.lcError textmerge noshow
Error No: << m.tnerror >>. Message: << m.tcMessage >>
Source: << m.tcProg >> at line << m.tnLineNo >>
    ENDTEXT
    Messagebox(m.lcError, 0+4096, "Error", 5000)
    Local loForm
    loForm = _vfp.ActiveForm
    If !Isnull(m.loForm)
        m.loForm.Release()
    Endif
Endproc

This would just release the active form.

Upvotes: 1

Related Questions