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!

LostFocus not executing code

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
508
Brasil
Hello colleagues!

In my form I have 2 fields to receive data from user:
LocalizadorVoucher_pfzybs.jpg

When the user completed typing in the first field, the application test if the second field is blank. If the second field is blank, it copies the contents from the first field to the socond field.

So I put the following code in the LostFocus event of the first field:

Code:
Object: txtLocalizador     Procedure: (event) LostFocus
IF SUBSTR(thisform.txtVoucher.Value,1,3)=SPACE(3)
	thisform.txtVoucher.Value=thisform.txtLocalizador.Value
ENDIF

It works fine, if the user press Enter after typing data in the first field. If the user press TAB (instead of Enter) the second field is not populated with the data from the first field.

When we press TAB to go out from a field, it has not to execute the LostFocus event?


Thank you,
SitesMasstec
 
Why are you testing if the first leading 3 characters are space characters?
It looks like this is the element that isn't executing properly to me.
Have you Set Step On at the lost focus event on the first field to trace what's happening?

Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
The event happens, but if thisform.txtVoucher.Value is empty, you don't get SPACE(3) you get the empty string ''.

SUBSTR() is not padding the result to 3 characters just because you say you want 3 characters.

Chriss
 
Oh, I am getting old.

Yes, Cris, I added: OR thisform.txtVoucher.Value="" and now it works:

Code:
IF SUBSTR(thisform.txtVoucher.Value,1,3)=SPACE(3) OR thisform.txtVoucher.Value=""
	thisform.txtVoucher.Value=thisform.txtLocalizador.Value
ENDIF

Thank you,
SitesMasstec
 
Good to see you have a solution. However, a better way to test for an empty string would be to use the EMPTY() function. That avoids any issues with leading or trailing spaces (or TABs), and also avoids possible confusion caused by SET EXACT.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Why do you even check the first three characters?
Why don't you use LEFT(thisform.txtVoucher.Value,3) for that? (It will also be a maximum of 3 characters, not exactly 3)
Why do you now only check the special empty case? If the other textbox would contain 2 spaces you would still not copy over the entered text.
Why don't you use THIS.VALUE inside the txtLocalizador textbox and instead use the long object name rooted in thisform?
There are several things I donÄt understand, but I would not necessarily change to using EMPTY, if there's a specific reason.

For example, you would also overwrite the value of thisform.txtVoucher.Value, if it starts with 3 spaces, but then contains text, for example ' test' would be "empty" in your definition of checking specifically the first 3 characters. Is this just overcomplicated thinking, like not using THIS when it's available, or is there some reason behind that?

Chriss
 
Yes Mike, it worked also. I changed the code to:

Code:
IF EMPTY(thisform.txtVoucher.Value)
	thisform.txtVoucher.Value=thisform.txtLocalizador.Value
ENDIF


Thank you,
SitesMasstec
 
Chris, thank you for those nice tips, too.

Of course, if the txtLocalizador is [tt]" 45678"[/tt] for example, it would be as it had nothing, because I just tested the first 3 characters, in my former code.

I also took note of your other tips. Indeed I was used to SUBSTR, explicitly code (thisform.txtLocalizador.Value instead of THIS.Value)

Thank you,
SitesMasstec
 
You could also use ALLTRIM() before testing.


Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top