B4J Question Update textfield, textarea or label in a loop

Pietro Pancino

Member
Licensed User
Longtime User
Hi everybody!
I wrote a function that send the content of a textarea line by line on a serial port (a loop in fact).
I wanted to display the progression in textfield like this "xxx % uploaded". When I Log the percent value with LOG data are displayed correctly each after others. When I update the value in a textfield, it's displaying the data only at the end of the loop.
Is there a function in B4J that allow to refresh all screen elements when I want.
For example in Lingo (director) there was "Updatestage"...

If any one can help!
:) Pietro
 

jmon

Well-Known Member
Licensed User
Longtime User
the way to do it is with CallSubDelayed, CallSubDelayed2, CallSubDelayed3:

B4X:
Sub SendData
    dim lines() as string = regex.split("/n", ta.text)
    for each line in Lines
        CallSubDelay2(Me, "ExecuteLine", line)
    next
End Sub

Sub ExecuteLine(Line as string)
    'send your line to the serial port
    tfProgress.text = "your progress here"
End Sub

You need to call the function with a delay, to allow the UI to refresh itself.
 
Upvote 0

Pietro Pancino

Member
Licensed User
Longtime User
Thanks for your answers!
Erel, I already use asyncstreams for serial port, it's working very well.
Jmon, I tried with CallSubDelay2, but it didn't work, we still stay freeze until the transfert is complete. I think it's because of the loop.
So I keeped your idea of delayed function using a timer.
When I started my upload function, I start a timer, and I used the timer_tick function to send lines one after each other.
In the _tick function I update a progressbar and the count of transfered line in a textfield and it's working very well!

Thank you all for your help!

:) Pietro



B4X:
public Sub upload_file(nom_fichier As String,edit As TextArea)
     upload_timer.Initialize("upload_timer_loop", Main.upload_delay)   
     serial.write_com_port_cr("load "&nom_fichier)
    ' init upload list
    upload_list.Initialize
    upload_list.AddAll(Regex.Split(CRLF, edit.text))
    upload_ptr = 0
    Main.upload_progress.Visible = True
    Main.upload_progress.Progress = 0
    upload_timer.Enabled = True
     Log("start upload")

End Sub
' Example:   setProgressBarValue(progressbarMain, 0.90)

Sub upload_timer_loop_tick
    upload_ptr = upload_ptr +1
    If upload_ptr = upload_list.Size Then
          upload_timer.Enabled = False
        serial.write_com_port_cr(Chr(3))
        Main.upload_progress.Visible = False
        Log ("end of upload")       
    Else
        Dim ch As String
        ch = upload_list.Get(upload_ptr)
        serial.write_com_port_cr(ch)       
    End If
   
    Main.t_progress_1.Text =upload_ptr& "/"& upload_list.Size
    Main.upload_progress.Progress = upload_ptr/upload_list.Size
End Sub
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Upvote 0
Top