B4J Question Trapping invalid formatted text in BBScrollingLabel

aminoacid

Active Member
Licensed User
Longtime User
I'm using a BBScrollingLabel in a B4J Application where a user enters a formatted text string in a textbox (txtInpup.text) and displays the string in a scrolling label:

B4X:
TextEngine.Initialize(MainForm.RootPane)
BBScrollingLabel1.TextEngine = TextEngine
smsg = txtInput.text
BBScrollingLabel1.Text = $"${smsg}"$

Works great. If the user enters an input string - For example :

"This is [colour=red] RED [/colour] and not BLACK"

(note: I purposely spelt color with a u in this post to suppress the formatting of the tags in this message. But it's actually "color" and not "colour" in my code )

It displays "This is RED and not BLACK" correctly

However, if the user enters a bad tag. For example - [/c] instead of [/colour]

"This is [colour=red] RED [/c] and not BLACK"

The program will crash with the message:

Error (position - 40): Closing tag does not match: c
Error occurred on line: 184 (BBCodeParser)
java.lang.RuntimeException: Object should first be initialized (List).


OK.. so I can trap the error using "Try-Catch" -

B4X:
smsg=txtinput.Text
    Try
        BBScrollingLabel1.Text = $"${smsg}"$
    Catch
        Log("Invalid tag in message")
    End Try

But once an error is trapped, on the next go-around, when the user tries to enter the corrected test string, - "This is [colour=red] RED [/colour] and not BLACK"

it appears that "BBScrollingLabel1.Text" has retained the previous value ( with the /c" tag) and the error gets trapped again with the same error message

Error (position - 40): Closing tag does not match: c.
Error occurred on line: 184 (BBCodeParser)
java.lang.RuntimeException: Object should first be initialized (List).


No matter what the user types in, an error is trapped and the above message is displayed until the program is restarted and a valid string is entered.

Is then any way to "clear" BBScrollingLabel1.text after an error is trapped? I tried reinitializing in the "Try-catch" section:

B4X:
TextEngine.Initialize(MainForm.RootPane)
BBScrollingLabel1.TextEngine = TextEngine

But that causes another crash.

Any thoughts?

Thanks!
 

aminoacid

Active Member
Licensed User
Longtime User
Thanks Erel! I have attached a simple project, "ScrollingLabel.zip" which reproduces the issue.

1. Start the program and the default message will display and scroll properly.

2. Enter a properly formatted string such as "This is [colo r=blue] BLUE [/colo r] and not BLACK" (remove space from colo r), click APPLY and the scroll text will change as expected.

3. Then edit the string to try a bad tag and change the [/colo r] tag to [/c]. Click APPLY. The error will be trapped.

4. Now re-enter a properly formatted string such as "This is [colo r=red] RED [/colo r] and not BLACK", click APPLY and you will see that the scroll text does not change and the same error is trapped again.
 

Attachments

  • ScrollingLabel.zip
    2.7 KB · Views: 185
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: you can use the plain tag in the forum to prevent it from breaking the tags.

Use this code:
B4X:
Sub Button1_Click
    Dim smsg As String = txtInput.text
    Try
        Dim data As BBCodeParseData
        data.Initialize
        data.Text = smsg
        TextEngine.TagParser.Parse(data)
        If TextEngine.TagParser.ErrorString.Length = 0 Then
            BBScrollingLabel1.Text = smsg
        End If
    Catch
        Log("Invalid tag in message")
    End Try
End Sub
 
Upvote 0
Top