Capture name of control that has focus

aerohost

Member
Licensed User
Hi, I'm trying to get the ID of a form control that has the current focus. It seemed to me the AddEvent in combination with the Sender function would do the trick, but it doesn't seem to work when I try the example in the help.

Here's the code:

Sub App_Start
AddEvent("cbShipName", Click, "FieldGotFocus")
End Sub

FieldGotFocus
Msgbox(Sender)
End Sub

Anyone have any ideas? Do I have to add a library or something like that?

Tks, AB :sign0161:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Which type of controls do you have on your form?
For controls that support the GotFocus event you can use something like:
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    AddEvent("TextBox1",GotFocus,"GotFocus")
    AddEvent("TextBox2",GotFocus,"GotFocus")
    AddEvent("TextBox3",GotFocus,"GotFocus")
End Sub

Sub GotFocus
    form1.Text = Sender
    Sender.Color = cGreen
End Sub
 

aerohost

Member
Licensed User
Hi Erel, thanks, that works perfectly, it's just what I needed.

But, I'm wondering why it doesn't work when I use the event Click instead of GotFocus? When I run it on the desktop and click the field, there's no response ...

Also, is there a list of the different event types somewhere?

Thanks, Adrian
 

aerohost

Member
Licensed User
Ok, thanks to you both, I get it. I was using a control that doesn't support the Click event - now I understand where to see the list of supported events for each control.

Regards, AB
 
Top