Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setfocus on first textbox control on page
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00944565
Message ID:
01034614
Views:
21
>Hi Nick, Nadya and others
>
>This is the code I use, it may not be the optimal code but it gets the job done. I had previously asked the same question and this code is based on a lot of feedback from the UT. If I am not mistaken Hilmar had a hand in getting this right for me, thanks. I also call this routine in my AfterNew(), AfterCancel() and AfterDelete() with parameter as .F., so please note you may have to modify it to works as per your framework.
>
>form.Activate()
>
IF THIS.lActivateRunOnce
>	THIS.lActivateRunOnce = .F.
>
>	THIS.SetFocusToFirstControl(.T.)
>ENDIF
>
>form.SetFocusToFirstControl()
>
LPARAMETERS tlInit
>
>#DEFINE CLASSLIST_DEF "CHECKBOX", "LISTBOX", "COMBOBOX", "COMMANDBUTTON", "OPTIONBUTTON", "SPINNER", "TEXTBOX", "EDITBOX", "GRID"
>
>IF tlInit
>	LOCAL lnLoop, lnClass, lnLoopPages, lnPagesControls, lcPageControlClass, lnPages
>
>
>	WITH THISFORM
> 		FOR lnLoop = .CONTROLCOUNT TO 1 STEP -1
>			lcClass = UPPER(.CONTROLS[lnLoop].BASECLASS)
>
>			IF INLIST(lcClass, CLASSLIST_DEF)
>				THIS.oFirstControl = .CONTROLS[lnLoop]
>			ELSE
>				IF lcClass ="PAGEFRAME"
>					lnPages = .CONTROLS[lnLoop].PAGECOUNT
>
>*!*						FOR lnLoopPages = lnPages TO 1 STEP -1
>						lnLoopPages = 1
>						
> 						FOR lnPagesControls = .CONTROLS[lnLoop].PAGES[lnLoopPages].CONTROLCOUNT TO 1 STEP -1
>							lcPageControlClass = UPPER(.CONTROLS[lnLoop].PAGES[lnLoopPages].CONTROLS[lnPagesControls].BASECLASS)
>
>							IF INLIST(lcPageControlClass, CLASSLIST_DEF)
>								THIS.oFirstControl  = .CONTROLS[lnLoop].PAGES[lnLoopPages].CONTROLS[lnPagesControls]
>							ENDIF
>						ENDFOR
>*!*						ENDFOR
>				ENDIF
>			ENDIF
>		ENDFOR
>	ENDWITH
>ENDIF
>
>
>THIS.pgf.ACTIVEPAGE = 1
>THIS.oFirstControl.SETFOCUS()
>
>Hope it helps. I also welcome suggestion if this code can be made to better perform or placed better in the classes/framework.

Well, you do THIS.pgf.ACTIVEPAGE = 1
Without it the first control is not selected the first time the form is activated. So, in order to not execute Page.Activate, Page.Deactivate and perhaps a plehora of other methods, I set ActivePage to 0 in design time. Now everything works fine for me and I avoided the overhead.

MereMortals framework has SetFocusToFirst method, which is very similar to what you wrote and uses a recursion.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform