Rick Strahl's Weblog
Rick Strahl's FoxPro and Web Connection Weblog
White Papers | Products | Message Board | News |

SetFocus() in Form Init() kills @K Select All Formatting on Textboxes

1 comment
January 04, 2012 •

Ran into a little nit today in some code for Help Builder. I have a small dialog in my toolbox that I use to create HREF links. Basically this dialog captures a title, URL and either returns those values back or creates an anchor (<a>) HTML tag from it.

The dialog looks like this:


Usually a user will be editing some help topic, then type some text like Visit our Support Site, highlight the selection of text and then pop into the dialog. Ideally I want to start out in the Web Link field and have the text highlighted.

So in the designer I have thisform.txtUrl.Format = "K" which autoselects the text in both controls. Now this all works fine as long as I tab through the controls manually. However, my original code used the original TabOrder and tried to SetFocus to the URL like this in the Init of the form class:

LPARAMETER lcUrl, lcText

THIS.cUrl = TRIM(lcUrl)
THIS.cText = lcText

*** Move focus to second field
IF !EMPTY(lcText)

This works fine for moving the focus - the control comes up with the focus set to txtUrl just fine. But unfortunately the @K auto-selection of the text is not happening. Using SetFocus() during the startup of the form simply fails. It only fails when using SetFocus() during startup in Init() or before the form is activated - if you call SetFocus() after activation the @K selection is properly respected.


Of course there's a simple and obvious workaround that probably would have been the better solution in the first place here. I can simply set the TabOrder to force the txtUrl field to be the first field accessed by the form and adjust the taborder accordingly with the txtTitle being the last in the taborder.

If you really need dynamic focusing at runtime, you can move that code to a later time in the form's event cycle: Moving it to the Activate event works.

Posted in: FoxPro    Desktop Apps

Feedback for this Weblog Entry

re: SetFocus() in Form Init() kills @K Select All Formatting on Textboxes

Frank Camp
January 06, 2012

Always respect Lisa G. (Load, Init, Show, Activate, GotFocus)

So here, you want the Setfocus just before the Gotfocus, which means the Activate is the most logical place (just don't forget to add code that it is only done the first time the activate gets called)

© Rick Strahl, West Wind Technologies, 2003 - 2024