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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Valid() with messagebox()

Status
Not open for further replies.

ldgreen

MIS
Oct 24, 2006
2
US
I have read several posts regarding this situation, but still cannot get it right. I have a text box, and when its value is outside certain value, it must be confirmed before proceeding. I am attempting this with a yes/no messagebox.
If response is no, control returns to textbox, but if yes, the messagebox fires again. All is ok when responding yes the second time, but if no the second time, messagebox fires several more times.
Thanks in advance for any help.
VFP 5.0
 
The behaviour you described is exactly what you would expect. Your Valid routine doesn't know that the message has fired before. It just goes ahead and does it again.

The solution is to set a flag the first time the message fires and the user answers No, and to refrain from doing it again when the flag is set.

You can't use a local variable for the flag, because it will go out of scope each time the Valid method exists. Instead, you should make it a property -- ideally of your base text box class. (If you don't have a base text box class, you can make it a property of the form.)

Let's suppose you call the property lChecked. In the text box's GotFocus, put:

THIS.lChecked = .F.

In the Valid, write something like this:

Code:
IF <value is out of range>
  IF NOT THIS.lChecked
   llReply = MESSAGEBOX("whatever")  
   IF llReply = 7  && No
     THIS.lChecked = .T.
     RETURN .F.
   ENDIF
  ENDIF
ENDIF 
RETURN .T.

I haven't tried to test the above code. It's just to give you the general idea.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
All in all you have a warning. If the user answers no, then doesn't change the value and tabs off the textbox, all is fine too.

I'd do such a warning(s) in a different strategy: In valid use a wait window "value out of range" nowait, return .T. anyway. And in the close or save button put a messagebox with all warning or error messages about the whole record. If the error is acceptable (user should have entered some value, but must not), you may save anyway. If there is an error set the focus to the wrong or missing value.

That way the user is reminded of errors but can move freely in the form anyway.

And in general:
Have one method you call from every valid event which checks the whole record. That way there is also no chance for contradicting rules within different controls, eg text1 may check if this.value>text2.value, text2 checks wheter this.value>text1.value and you never can fullfill both validation rules at once.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top