Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling

Status
Not open for further replies.

HanzieV

Programmer
Apr 8, 2003
17
0
0
BE
Hey,

is it possible to have all objects on a form to use the error event of the form in stead off their own error event without having to call the forms error routine from within each object. For example: if I have a form with a command button, and the code behind the click event of the button generates an error, what do I have to do to trigger the error routine of the form and not of the command button ??
 
HanzieV,

I'm ready to stand corrected by someone, but my understanding is that the way the error events work is that the error is passed up the class hierarchy not the object hierarchy.

So your command button object would, by inheritance, call its parent class error event and so on. I think that if the base class error event has no code, the error gets passed to any global error handler or the dreaded VFP Cancel, Retry, Ignore.

So I don't think what you want is possible without adding code to each object's error event. In fact of course you wouldn't do this in the object but in the base class.

Why do you want to have the form's error event called rather than each object?

I suspect there would be little advantage in directing errors to the form error event rather than just using a global ON ERROR handler.


Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Here's a method.
Note the form's reference is visible by the global error handler.

Darrell

Code:
private oForm
oForm = createobject("MyForm")
oForm.show()

on error do ErrHand with error(), program(), lineno()

read events

on error

procedure ErrHand
  lparam nErr, cMeth, nLine
  oForm.error(nErr,cMeth,nLine)
endproc


define class MyForm as form
  docreate = .t.
  autocenter = .t.
  height = 100
  caption = "Indirect method to trigger form error event"

  cSource = ""

  add object txtThrowError0 as textbox with ;
    top = 10, ;
    left = 10, ;
    width = 100, ;
    height = 27, ;
    controlsource = "thisform.cSource"

  add object lblInstruct as label with ;
    top = 40, ;
    left = 10, ;
    width = 100, ;
    wordwrap = .t., ;
    height = 200, ;
    caption = "Enter 'hello' in textbox to cause an error"

  add object cmdThrowError1 as commandbutton with ;
    top = 10, ;
    left = 120, ;
    width = 100, ;
    height = 27, ;
    caption = "Throw Error 1"

  add object cmdThrowError2 as commandbutton with ;
    top = 10, ;
    left = 240, ;
    width = 100, ;
    height = 27, ;
    caption = "Throw Error 2"


  procedure destroy
    clear events
  endproc

  procedure error
    lparameters nError, cMethod, nLine
    messagebox( ;
      "Error:  "+trans(nError)+chr(13)+;
      "Source: "+cMethod+chr(13)+;
      "Line:   "+trans(nLine), 16, "Error!";
      )
  endproc

  procedure txtThrowError0.lostfocus
    if lower(allt(this.value)) = "hello"
      ? ThrowError0
      this.value = ""
    endif
  endproc

  procedure cmdThrowError1.click
    ? ThrowError1
  endproc

  procedure cmdThrowError2.click
    ? ThrowError2
  endproc

enddefine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top