Monday, February 4, 2013
Setting Focus to an Object via its TabIndex Value
MK has been repeatedly requesting from me if I can make ssOptSwitch totally loose focus and moves to the next control when user presses TAB and I said that unless someone showed me a way to do that via tabindex, then it can't be done. No one showed me so it stays that way.
That default attitude of the class is not surprising considering ssOptSwitch actually is just a container comprised of several ssSwitch so it is just natural that when one switch looses focus via Enter or Tab keys, then the next switch based on tab index will receive it. And I really cannot imagine yet how to do something like:
I was sitting in front of my laptop Sunday afternoon as I am in the mood going through the minor bugs on Alpha 3.0 as mentioned by MK fixing those one by one until I came to his ssOptSwitch Tab request. Suddenly I realized I have been saying all along the solution to that and that the above command can actually be translated into something like this:
* ssOptSwitch Keypress Event
Lparameters nKeyCode, nShiftAltCtrl
If m.nKeyCode = 9 && tab or enter
For Each loCtrl In This.Parent.Controls
If loCtrl.TabIndex = This.TabIndex + 1
When your mind is free and you are not cramming to do a lot of jobs, ideas really comes easy to mind.
Unfortunately, nKeyCode do not differentiate between Tab and Enter keys there. They are both 9. So pressing Enter or Tab key will now set focus to the next control following ssOptSwitch via TabIndex. I would have loved having Enter key to move from one switch to the next and Tab for totally going out of the class moving to next control but... Oh well..... we can still use the arrow keys anyway for that plus spacebar.
You may ask me why a post for this small thing? Well it took me a while to figure out this simple trick and though I doubt it can have other usage on your end, it is just fun to know that we can do the setting of focus this way, i.e., via TabIndex value. Plus I won't be updating Alpha 3.0 with fixes yet so if you want to add this feature now on your end, you can do so. Cheers!