B4J Question Update text field from string name

DavidMeees

Member
Licensed User
Hi I am looking to get the text from a textfield when the textfields name is stored in a string. I was able to do this somehow in VBnet

For example
PrintingModule.SSCells(2,0) = "TextServiceDate" & .text

I am new to B4J and have looked the forum for hours to try and find a solution

Thanks
 

PaulMeuris

Well-Known Member
Licensed User
You have a couple of options to get or set the text from a text field.
1. You can set the name of the view (Textfield1) in the Tag property of the view in the Visual Designer and use this code:
B4X:
    For Each vw As B4XView In Root.GetAllViewsRecursive
        If vw.tag = "TextField1" Then
            Dim tf As TextField = vw
            Log(vw.Tag & " text: " & tf.Text)
            tf.Text = "new text"
        End If
    Next
The Root is in this case the parent of the TextField1 view.
It could also be a pane and then you use that view to search for the Textfield1 view.
To check that the text has changed you could use of course:
B4X:
Log("new text in TextField1: " & TextField1.Text)
2. You can get the view directly if you know the index of the view in the parent container (Root or Pane).
The first view has index 0:
B4X:
Log("getting the first view text: " & Root.GetView(0).Text)
1705987823396.png
1705987784747.png

The Tag property is set and the TextField1 view belongs to Main (=Root).
1705988121884.png
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
the textfield's name is stored in a string

Why?

Also, are there supposed to be quotes around the ".text" ?

PrintingModule.SSCells(2,0) = "TextServiceDate" & .text

but if so, then why not just:

PrintingModule.SSCells(2,0) = "TextServiceDate.text"

or is it supposed to be:

PrintingModule.SSCells(2,0) = TextServiceDate.text

which would work like a charm in B4J too (assuming PrintingModule has an array SSCells(,) and your program has a Dim TextServiceDate As TextField or B4XView).
 
Upvote 0

DavidMeees

Member
Licensed User
I have the textfield name stored in a string, I am sorry I just used the string hoping to Simplify the question
it should be PrintingModule.SSCells(2,0) = (Name in string in).text. The textbox name is saved in a data field which I transfer into an array
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't quite understand what you want to achieve (also because the time is late here!), however perhaps a Map can be useful to you, which can have any type of object as keys, including Views (TextFields/EditText/B4XView, ...). The associated value could be text, in your case.

mapTextFields.Put(TextField1, "LucaMs is mad") ' Strangely if you write: "Log(mapTextFields)" it will be: "True" ?
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I don't quite understand what you want to achieve (also because the time is late here!), however perhaps a Map can be useful to you, which can have any type of object as keys, including Views (TextFields/EditText/B4XView, ...). The associated value could be text, in your case.

mapTextFields.Put(TextField1, "LucaMs is mad") ' Strangely if you write: "Log(mapTextFields)" it will be: "True" ?
Yes, Map can be use here.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is a quick example if I understand correctly.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TextArea1 As B4XView
    Private TextField1 As B4XView
    Private TextField2 As B4XView
    Private TextServiceDate As B4XView
    Private TextViews As Map
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Form")
    MainForm.Show
    TextViews = CreateMap("TextField1": TextField1, "TextField2": TextField2, "TextServiceDate": TextServiceDate, "TextArea1": TextArea1)
    PrintingModule.Initialize(10, 10)
End Sub

Private Sub BtnStore_Click
    PrintingModule.SSCells(2, 0) = TextView("TextServiceDate").Text
    Log($"Value stored in SSCells(2, 0)=${PrintingModule.SSCells(2, 0)}"$)
    PrintingModule.SSCells(2, 2) = TextView("TextArea1").Text
    Log($"Value stored in SSCells(2, 2)=${PrintingModule.SSCells(2, 2)}"$)
End Sub

Private Sub TextView (Key As String) As B4XView
    Return TextViews.Get(Key)
End Sub
 

Attachments

  • B4X.zip
    2.6 KB · Views: 197
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I think this example does what you want ( unfortunately you have to set the id for each node you want to lookup)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private TextField1 As B4XView
    Private scene As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    scene = (MainForm.RootPane).As(JavaObject).RunMethodJO("getScene",Null)
    setID(Button1,"OnlyButton")
    setID(TextField1,"TextField1")
   
    Log(getID(Button1))
    Log("log Button1 = " & Button1)
    Log("lookup      = " & lookUp("OnlyButton"))
    lookUp("OnlyButton").Text = "Press Me"
    Log("Btn txt = " & lookUp("OnlyButton").Text)
   
    ' set textfield text to buttons text
    lookUp("TextField1").Text = lookUp("OnlyButton").Text
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub

Sub setID(n As Node, id As String)
    (n).As(JavaObject).RunMethod("setId",Array(id))
End Sub

Sub getID(n As Node) As String
    Return (n).as(JavaObject).RunMethod("getId",Null)
End Sub

Sub lookUp(s As String) As B4XView
    Return scene.RunMethod("lookup",Array("#" & s))
End Sub

If the designer set the ID to the nodes name when creating the layout, this becomes trivial to find any control by string name, just like css selectors.
 
Last edited:
Upvote 0
Top