Page 2 of 2

Re: Weird Behavior of OnLostFocus

Posted: Thu Sep 20, 2012 10:51 am
by Jayesh
Rathi,
is lostfocus event not work well with 'Setfocus' method ? I am experiencing some problem using 'Setfocus' in lostfocus event.
Can u help me plz ?

My sample code is as above , I want to setfocus of text_1 field if text_2 value is 100

Thanks in advance.


#include "hmg.ch"
*
Function Main

DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 940 HEIGHT 480 ;
TITLE 'Lostfocus Test' ;
MAIN

@ 10,10 LABEL lbl_ERRMSG ;
VALUE ""



@ 110,10 TEXTBOX Text_1 ;
VALUE 123 ;
NUMERIC ;
ON LOSTFOCUS SHOWMSG1("text_1")



@ 150,10 TEXTBOX Text_2 ;
VALUE 123 ;
Numeric ;
ON LOSTFOCUS SHOWMSG1("text_2")

@ 200,10 TEXTBOX Text_3 ;
VALUE 123 ;
Numeric ;
ON LOSTFOCUS SHOWMSG1("text_3")


END WINDOW

Form_1.Center

Form_1.Activate

Return Nil
*******************************
FUNCTION SHOWMSG1(mxErrMsg)
mtmp := space(10)
mxyWindName := ThisWindow.Name
mxyCntrName := "Lbl_ERRMSG"
SetProperty(mxyWindName,mxyCntrName,"Value","Displaying "+mxErrMsg)
mxSec := Seconds()
DO WHILE (Seconds() - mxSec) < 1
ENDDO
SetProperty(mxyWindName,mxyCntrName,"Value","")
*
mTmpVal := Form_1.Text_2.value
IF (mxErrMSg = "text_2") .and. (mTmpVal = 100)
mxyCntrName := "Text_2"
DOMETHOD(mxyWindName,mxyCntrName,"SetFocus")
ENDIF
*
RETURN NIL
**********************************

Re: Weird Behavior of OnLostFocus

Posted: Thu Sep 20, 2012 11:50 am
by Pablo César
Some changings was necessary, please note corrections:

Code: Select all

#include "hmg.ch"
*
Function Main

DEFINE WINDOW Form_1 ;
	AT 0,0 ;
	WIDTH 940 HEIGHT 480 ;
	TITLE 'Lostfocus Test' ;
	MAIN 

	@ 10,10 LABEL lbl_ERRMSG ;
		VALUE "" 

	@ 110,10 TEXTBOX Text_1 ;
		VALUE 123 ;
		NUMERIC ;
		ON LOSTFOCUS SHOWMSG1("text_1")

	@ 150,10 TEXTBOX Text_2 ;
		VALUE 123 ;
		Numeric ;
		ON LOSTFOCUS SHOWMSG1("text_2")

	@ 200,10 TEXTBOX Text_3 ;
		VALUE 123 ;
		Numeric ;
		ON LOSTFOCUS SHOWMSG1("text_3")


END WINDOW
Form_1.Center
Form_1.Activate
Return Nil

FUNCTION SHOWMSG1(mxErrMsg)
// mtmp := space(10)
// mxyWindName := ThisWindow.Name
// mxyCntrName := "Lbl_ERRMSG"
SetProperty("Form_1","Lbl_ERRMSG","Value","Displaying "+mxErrMsg)
mxSec := Seconds()
DO WHILE (Seconds() - mxSec) < 1
ENDDO
SetProperty("Form_1","Lbl_ERRMSG","Value","")
*
mTmpVal := Form_1.Text_2.value
IF (mxErrMSg = "text_2") .and. (mTmpVal = 100)
   // mxyCntrName := "Text_2"
   DOMETHOD("Form_1","text_1","SetFocus")
ENDIF
*
RETURN NIL

Re: Weird Behavior of OnLostFocus

Posted: Fri Sep 21, 2012 4:51 am
by Jayesh
Thanks Pablo César for prompt reply.

Sorry, it's my mistake that i haven't given the exact problem that i'm facing using lostfocus. i want to use 'Setfocus' in 'lostfocus' event. In my sample program, i want to take back control from text_2 to text_1 if the value entered in text_2 is 100. The problem is that after punching 100 in text2 it first displays the message of text_3 (plz. check display label) and then cursor goes to text_1. Pablo, I tried ur code, but it gives the same problem.

Plz. help.

Regards.

Re: Weird Behavior of OnLostFocus

Posted: Fri Sep 21, 2012 5:36 am
by Rathinagiri
Hi,

In your code,

mxyCntrName := "Text_2"

Should be,

mxyCntrName := "Text_1"

Regarding the message of 'text_3', there is no intermediate state in windows. ie., after the control is lostfocus in text2, text3 will get focus immediately since that is the next control in the tab order. Now, if you don't want to process the whole procedure for text3 lostfocus event, we can do it manually. Please consider the following code:

Code: Select all

#include "hmg.ch"
*
Function Main
public lText3Focus := .f.

DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 940 HEIGHT 480 ;
TITLE 'Lostfocus Test' ;
MAIN

@ 10,10 LABEL lbl_ERRMSG ;
VALUE ""



@ 110,10 TEXTBOX Text_1 ;
VALUE 123 ;
NUMERIC ;
ON LOSTFOCUS SHOWMSG1("text_1")



@ 150,10 TEXTBOX Text_2 ;
VALUE 123 ;
Numeric ;
ON LOSTFOCUS SHOWMSG1("text_2")

@ 200,10 TEXTBOX Text_3 ;
VALUE 123 ;
Numeric ;
ON LOSTFOCUS SHOWMSG1("text_3")


END WINDOW

Form_1.Center

Form_1.Activate

Return Nil
*******************************
FUNCTION SHOWMSG1(mxErrMsg)
if lText3Focus
   return nil
endif

mtmp := space(10)
mxyWindName := ThisWindow.Name
mxyThisCntr := This.Name
mxyCntrName := "Lbl_ERRMSG"
if upper( mxyThisCntr ) == 'TEXT_2'
   lText3Focus := .t.
else
   lText3Focus := .f.
endif
SetProperty(mxyWindName,mxyCntrName,"Value","Displaying "+mxErrMsg)
mxSec := Seconds()
DO WHILE (Seconds() - mxSec) < 1
ENDDO
SetProperty(mxyWindName,mxyCntrName,"Value","")
*
mTmpVal := Form_1.Text_2.value
IF (mxErrMSg = "text_2") .and. (mTmpVal = 100)
mxyCntrName := "Text_1"
DOMETHOD(mxyWindName,mxyCntrName,"SetFocus")
ENDIF
*
RETURN NIL
**********************************