Writing multiline messages to a textbox which won't scroll (& general question re B4P support)

mikethebike

Member
Licensed User
Specific question: I'm sending a stream of one-line messages to a textbox but I can't get it to scroll. CRLF just homes the cursor in the box and the new message overwrites the old. This has to be straightforward, so any pointers please?

General question: About 3 years ago I used B4PPC on a specific project to display and record serial data from an Arduino which is attached to sensors on our rainwater tanks, solar thermal and PV panels and wood boiler and it was just brilliant. Coming back to it now however, I see everything's been eclipsed by B4A (which is perfectly understandable), but I can't find the original (excellent) on-line B4P documentation (maybe I haven't looked hard enough), and it's also not possible to search just the B4P forum, yet B4P is still available for purchase to newcomers.
Is this really the case, or am I missing something?
Thanks!
Mike
 

mikethebike

Member
Licensed User
I should have given some more detail - doh! I'm using a Sports Radar gun to measure and record the speeds of vehicles passing through our village. This sends a 17 byte packet of serial data (including the speed) when it detects a vehicle passing, which is passed directly into the PDA's serial port. The serial data triggers an OnComm event, I extract the data within this subroutine, and output the time and speed to a TextBox (set up for multiline). The problem is that, tweak as I may, each time the data appears in the TextBox, the cursor has homed so only the top line is ever used.

I can add text on lower lines but only by inserting CRLF between the strings in the same TextBox1.Text statement, e.g.:

TextBox1.Text = "line 1" & CRLF & "line 2" & CRLF & "line 3"

prints "line 1", "line 2" and "line 3" on separate lines, but:

TextBox1.Text = "line 1"
TextBox1.Text = "line 2"
TextBox1.Text = "line 3"

are all output on the first line so in practise it's only the "line 3" that's visible as each line overwrites the last.

Again, any advice appreciated, I must be missing something fundamental!

Mike
 

mikethebike

Member
Licensed User
The "old" website is available here: http://www.b4x.com/specifications.html
Fantastic, thanks for that Erel, it's very reassuring that it's still available.
If it isn't already (I didn't see it but it wouldn't be the first time ...) I'd suggest linking it from the main www.b4x.com webpage for those who still dabble in the old stuff - PPC's are still commonly available on eBay and they're a great front end to an Arduino or Pi!
Best wishes
Mike
 

mikethebike

Member
Licensed User
TextBox1.Text = "line 1"
TextBox1.Text = "line 2"

In this case line1 is replaced by line2, normal behaviour.

TextBox1.Text = "line 1"
TextBox1.Text = TextBox1.Text & CRLF & "line 2"

Adds line2 in the second line.

Best regards.

Thanks Klaus
Yep, exactly what I discovered, but on further investigation I now realise that a TextBox is intended more for user INPUT than output, and if you input text it does autoscroll.
It may not be possible, but if there is a way to autoscroll text (sent via separate commands) on an OUTPUT box I'd be very grateful to know!
Mike
 

agraham

Expert
Licensed User
Longtime User
I can't check on an actual device as I no longer have one but if you set the Textbox to Multiline in the Designer then it should add a vertical Scrollbar as you add more lines. You can use ScrollToCaret to position the viewing point in the Textbox but the key is that the Textbox must have the focus to display the caret. I can't post a project as your version of Basic4ppc probably won't read the files from my own custom desktop version but try this code on a Form with a Textbox and two Buttons.

B4X:
Sub Globals
    'Declare the global variables here.
    X = 1
End Sub
 
Sub App_Start
    Form1.Show
End Sub
 
Sub Button1_Click ' Add some text'
    For i = 0 To 20
    TextBox1.Text = TextBox1.Text & CRLF & " Line" & x
    x = x + 1
    Next   
End Sub
 
Sub Button2_Click ' Scroll to the bottom
    TextBox1.Focus
    TextBox1.SelectionStart = StrLength(TextBox1.Text)
    TextBox1.SelectionLength = 0
    TextBox1.ScrollToCaret
End Sub
 

mikethebike

Member
Licensed User
I can't check on an actual device as I no longer have one but if you set the Textbox to Multiline in the Designer then it should add a vertical Scrollbar as you add more lines. You can use ScrollToCaret to position the viewing point in the Textbox but the key is that the Textbox must have the focus to display the caret. I can't post a project as your version of Basic4ppc probably won't read the files from my own custom desktop version but try this code on a Form with a Textbox and two Buttons.

B4X:
Sub Globals
    'Declare the global variables here.
    X = 1
End Sub

Sub App_Start
    Form1.Show
End Sub

Sub Button1_Click ' Add some text'
    For i = 0 To 20
    TextBox1.Text = TextBox1.Text & CRLF & " Line" & x
    x = x + 1
    Next  
End Sub

Sub Button2_Click ' Scroll to the bottom
    TextBox1.Focus
    TextBox1.SelectionStart = StrLength(TextBox1.Text)
    TextBox1.SelectionLength = 0
    TextBox1.ScrollToCaret
End Sub


Brilliant, thank you so much!
What never occurred to me is that a textbox string is just like any string in the sense that if you simply assign something to it you overwrite its previous value (what I was doing before), but if you concatenate your new text onto the original (TextBox1.Text = TextBox1.Text + newtext), you retain the original as well. The scrolling works really well now, but I guess I must periodically delete old data from the beginning which, whilst it doesn't appear in the textbox, is still stored. I'll be writing it onto an SD card anyway so it won't be lost.
A simple but invaluable lesson learned, very much appreciated!
Best wishes
Mike
 
Top