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!

Need advise on how to SetFocus on objects 5

Status
Not open for further replies.

HoffaFocus

Technical User
Dec 2, 2000
7
0
0
SE
According to the VFP-roules it is not allowed to set the focus on another object within the methods when,valid,rangehigh and rangelow. In my applications however it is a need to set focus on the next object as soon as the user has entered (textbox) or coosen (combobox) a value. The most natural way to think in my oppinion is to put the Object.SetFocus in the valid method, but as said this is not allowed. Now I'm spending more time on trying to find a proper way of setting focus on objects then on other tasks within the applications. This is realy frustrating and waste of time. For comboboxes it sometimes works with setfocus in the valid method and sometimes not. To work with Tagindex and Tabstop=.T. do not help in all occasions. Can someone help me where I can find a proper and simple description how to set focus on objects.
 
Hi,
Have you tried LostFocus event? pls do any checks in Valid, just put Object.SetFocus command in LostFocus

Jimmy

 
Think of the When, GotFocus, and Valid, Lostfocus methods this way;

When: Determine if focus will be allowed to arrive at the object (Return .T. allows focus to arrive, .F. disallows focus)

GotFocus: React to focus arriving (GotFocus will not fire unless When Returns .T.)

Valid: Determine if focus will be allowed to leave an object (Return .F. or 0 Disallows focus from leaving the object, Return .T. or non-0 numeric (see help file) allows focus to leave)

LostFocus: React to focus leaving an object (Lostfocus wil not fire unless Valid returns .T. or a non-0 numeric value)

Remember that LostFocus has a default behavior of moving focus to the object specified by the Valid's return value, so if you choose to use a SetFocus call in there you should also include a NODEFAULT command to frustrate the default behavior.

JimB

JimBooth
jbooth@jamesbooth.com
 
For comboboxes, you can force the focus to an object in the Click code. This makes sense. As Jim suggests, use LostFocus for evaluating textboxes. The Click or InteractiveChange methods would be ok, but no validation happens, in those cases, if the user mouses to a new object.

Good luck
Dan Dan Walter
DWalter@zoo.uvm.edu
 
This isn't a Answer but More to add to the Confusion!!
I thought this was the answer to my problems when I saw Jim Booth's Tek-Tip but it didn't work for me. All my forms reference a object to go to in Valid statement depending what the user does/Validation. I created a Form Property call 'SetFocusto' and in my Valid statements put the reference in this property.
EXAMPLE THISFORM.SetFocusto="THISFORM.LstStru16.Setfocus"
Then in all the Lostfocus Event I put the following generic code.
NODEFAULT
IF NOT EMPTY(THISFORM.Setfocusto)
*Macro '&' doesn't work on form properties
LOCAL nextobject
nextobject=THISFORM.Setfocusto
THISFORM.Setfocusto=""
&nextobject
ENDIF
Anyway I thought this would was the solution but it seems how gets the next focus is sometimes the previous code and most of the time the next TAB Order Object.
I also found out that the Setfocus to the object always sends the program sequence to the object but then most of the time returns to the Valid statement and run it completly again. This isn't good because we just lost the Setfocusto and by this time I have changed some of the enviroment.
I put Trace On and Wait Statements to track what was happening. I hope we get more help here.
 
Hi!

Just remove 'NODEFAULT' from LostFocus event.
Following are some explanations of behaviour you encountered.
NODEFAULT causes object to do not lose focus at all (same as if Valid returns .F.). Than you try to set focus to another control, so, Valid fired again, because object focus losing cancelled by NODEFAULT.
SetFocus to another control does not works because usual sequense of internal Windows/VFP events has been broken in LostFocus event. It is usual for Windows, not for VFP, and better do not make such complex actions with focusing, becaus often this causes "unpredictable" results.

Finally, some words about your solution.
In old VFP interfaces there was nice Valid event functionality allowed to focus any control of form conditionally. If Valid event returns 0, control remains focused. If Valid event returns 1, next control is focused. If Valid returns, for example, 5, focus set to the 5-th control in tab order starting from current. If Valid returns -<number>, foces set to the control in backward tab order and with appropriate number of controls skipped.
It looks like in your case much better will be use the way with number returned from Valid event. However, in such case keep an eye on new controls and change of tab order.

Hope this helped.


Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
I would change some things in your approach. In the valid when you detect that you want focus to move to a control do this;

Thisform.Setfocusto = Thisform.Text1 && an example object

Then in the lostfocus do something like this;

IF TYPE(&quot;THISFORM.Setfocusto.Name&quot;) = &quot;C&quot;
NODEFAULT
THISFORM.Setfocusto.SetFocus()
THISFORM.Setfocusto=NULL
ENDIF

JimB


JimBooth
jbooth@jamesbooth.com
 
JimB, one correction:
IF TYPE(&quot;THISFORM.Setfocusto.Name&quot;) = &quot;C&quot;

should be

IF TYPE(THISFORM.Setfocusto+&quot;.Name&quot;) = &quot;C&quot;

Again, I think NODEFAULT is not needed here. NODEFAULT in LostFocus causes control not lose focus, and this, next setfocus call will cause VALID of control fire again.


Vlad Grynchyshyn
vgryn@softserve.lviv.ua
The professional level of programmer could be determined by level of stupidity of his/her bugs
 
The NODEFAULT is needed to stop the default behavior of the LostFocus which is to move focus to the next control (or the one pointed to by the return value of Valid). The SetFocus call will move the focus for you, but if the NODEFAULT is not specified then after the SetFocus moves it the default behavior will move it elsewhere.

BTW, your correction to the sample code is appreciated, thanks.

JimB



JimBooth
jbooth@jamesbooth.com
 
Thanks Guys this was fun but it took me many days to prefect the ability to Setfocus to any object at will.
I used everyones thoughts and ideals and here is how I got things to work.
I used the Setfocus Command in the Lostfocus event with the following generic code.
Note: 'SetFocusTo' is a Form property I created.
Code:
IF NOT EMPTY(THISFORM.Setfocusto)
   NODEFAULT
   LOCAL nextobject
   nextobject=THISFORM.Setfocusto
   THISFORM.Setfocusto=&quot;&quot;
   &amp;nextobject
ENDIF
In all the Valid Methods the form property SetFocusTo is stuffed with the Reference to the intended Object.
Example: THISFORM.SetFocusTo=&quot;THISFORM.CmgWhatnext23.Setfocus&quot;
Now here's how I got things to happen has predicted.
In all the Valid Methods the command 'RETURN 0' is used to keep the current object from loosing focus (sometimes they do and sometimes they don't without this command). The next step was to cause the current object to loose focus without firing the Valid Method again. I simply stuffed the 'TAB' keystroke into the keboard before the 'RETURN 0' was executed. Example:
Code:
KEYBOARD &quot;{TAB}&quot; CLEAR
RETURN 0
When the focus was returned to the current object the keystroke 'TAB' was then executed causing the object to loose focus without firing the Valid Method again. Because there is something in the THISFORM.SetFocusTo property it is executed in the LostFocus Method.

Note this VPF behavior: If you Disable the current object in any of the current objects Methods the lostfocus method reverts back to the Default behavior (even though you have NODEFAULT in the Lostfocus method) which is the TAB Order and the focus goes to the next object in the TAB order.
 
Textbox doesn't behave the as Combobox, Editbox, Command buttons. It seems the valid method is fired very time you leave, no manner how the user leave (Mouse, Tab, Enter, etc.). My application didn't have any textbox's in it when I came up with the previous solution. I am use to X-base valid statements where the valid statement isn't fired unless something has changed in the Textbox. Any be awere of this.
 
Hi..
All the messages and replies... :)

The easiest way to set the focus to next object is to set the tab order properly in that order. Is this not so easy!

I am a new entrant to this forum - only a day old.. (not for foxpro)
 
Hi..
All the messages and replies... :)

The easiest way to set the focus to next object is to set the tab order properly in that order. Is this not so easy!

However if a conditional focus is needed, put that condition in the when event of that object.

I am a new entrant to this forum - only a day old.. (not for foxpro).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top