B4J Question [SOLVED] Global event (FocusChanged) for alla TextField with different events name

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
How I can get a single event (FocusChanged) for all TextFields in a project, also if the names assigned to TexFields events are different?
Thanks
 

Daestrum

Expert
Licensed User
Longtime User
You can use reflection and add a changelistener to the focusedProperty.
Example
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Dim tf1,tf2,tf3 As TextField
    Dim rf As Reflector
    tf1.Initialize("tf1")
    tf2.Initialize("tf2")
    tf3.Initialize("tf3")
    MainForm.RootPane.AddNode(tf1,10,10,-1,-1)
    MainForm.RootPane.AddNode(tf2,10,40,-1,-1)
    MainForm.RootPane.AddNode(tf3,10,70,-1,-1)
    rf.Target = tf1
    rf.AddChangeListener("focus","focusedProperty")
    rf.Target = tf2
    rf.AddChangeListener("focus","focusedProperty")
    rf.Target = tf3
    rf.AddChangeListener("focus","focusedProperty")
End Sub
Sub focus_Changed(oldvalue As Object,newvalue As Object)' for all textfields
    Dim tempTF As TextField = Sender' who was it
    tempTF.Text = newvalue
End Sub
' the following subs are triggered because focus_changed changes the text
' but they will act as normal if you change the text
Sub tf1_TextChanged (Old As String, New As String)' only for tf1
    Log("tf1 text = "&New)
End Sub
Sub tf2_TextChanged (Old As String, New As String)' only for tf2
    Log("tf2 text = "&New)
End Sub
Sub tf3_TextChanged (Old As String, New As String)' only for tf3
    Log("tf3 text = "&New)
End Sub
 
Upvote 0
Top