A way to return the name of the control that currently has focus?

Tex8503

Active Member
Licensed User
Longtime User
So I'm looking for a way to return the name of the control that the point is currently focused to so I can use it programmatically. Is there a way to do this?
 

Cableguy

Expert
Licensed User
Longtime User
Most of the "visual" controls has an event called "GotFocus", triggered when the control ...well...get the focus, and another called "LostFocus"...I bet you know waht that means...
I know its a nag to code a couple dozen GotFocus events to known wich got the focus, but with some code "teaks a tricks" I bet you can come up with a couple of lines of code to pull it out.
 

Tex8503

Active Member
Licensed User
Longtime User
Cableguy - the 'nagging' part isn't what bothers me - but rather it seems like such a counter-intuitive way of doing it. It would also introduce a great deal of bloat into the program as they get bigger.
 

mjcoon

Well-Known Member
Licensed User
So I'm looking for a way to return the name of the control that the point is currently focused to so I can use it programmatically. Is there a way to do this?

But when would you use it? The subsequent discussion about events shows that when a focus event occurs that is the time to find which control is involved. Otherwise you are just proposing some random or timer event to enquire where the focus is, and the user could swop to another control in the next instant and invalidate the answer.

A single event Sub can be related to all appropriate controls (using a loop, perhaps) and then that single Sub can find which control has the focus. Presumably different actions would then be required (context menu or whatever).

Mike.
 

Tex8503

Active Member
Licensed User
Longtime User
Actually - I do have a random event that this is useful for - My barcode scanner.

Rather than having to maintain 'gotfocus' events for each element - it'd be great if my scanning function could tell where focus was on demand to be able to plugin the data it has...
 

mjcoon

Well-Known Member
Licensed User
Actually - I do have a random event that this is useful for - My barcode scanner.

Rather than having to maintain 'gotfocus' events for each element - it'd be great if my scanning function could tell where focus was on demand to be able to plugin the data it has...

Well, perhaps, but that is not a common enough scenario for it to be an "intuitive" way of working.

Although I worked on a commercial system that included barcode scanning (for all sorts of purposes, not just merchandise), there was no similar link with the GUI.

So I think that coding as suggested using an event Sub is a reasonable solution.

Mike.
 

Tex8503

Active Member
Licensed User
Longtime User
My objection to this method is that I don't think its just one sub event (correct me if I'm wrong...) but I'd need a sub event for every object that I want to detect if it has focus or not...

If there were one or two elements, that might be ok... but I could run into scenarios where it could be 20+ elements. That ends up just being a poor way to program it.
 

mjcoon

Well-Known Member
Licensed User
My objection to this method is that I don't think its just one sub event (correct me if I'm wrong...) but I'd need a sub event for every object that I want to detect if it has focus or not...

You do need to do an AddEvent for each control (hence I mentioned a loop), but can cite the same Sub and then use Sender (see the Help for Runtime control manipulation) to detect which control has received the focus.

Mike.
 

Tex8503

Active Member
Licensed User
Longtime User
So... I was able to use one of Erel's examples to provide a decent solution that I'm satisfied with for this particular problem and figured I should share...

Check out Erel's example here: http://www.b4x.com/forum/questions-help-needed/4683-control-navigation-hardware-keys.html

You can use Hashtable1.Item(SelectedControl) to grab the name of the item that currently has focus. Yes - this does create gotfocus events for each item (which is less than desirable) - but this is a bit more clean than doing them each by hand...

This fixed another issue I was having with items not tabbing in the correct order (or at all...) as I was able to capture the tab button when pressed on the form and I'm using this code that Erel wrote to change focus to the next form element.
 
Top