B4J Question Autocolor text

ThRuST

Well-Known Member
Licensed User
What is an effective way to autocolor text in i.e. a HTMLEditor textbox and have certain words colored?
I've done this years ago in C++/CLI and with hundreds of words it took a long time to process every word so there must be a more effective way to do it than to loop through all words. One idea might be to reset a counter value each time the user presses spacebar and store what is typed in a string untill spacebar is pressed again then check the string for a matching word and color what's being typed by checking the start and lenght of the string, that way each words can be processed on the fly as you type. That could perhaps remove the looping process which tends to be very time consuming. Since autocoloring of text is implemented in the B4X IDE I kindly ask Erel for advice on how to do this, but everybodys comments are of course welcomed so that others can learn how to do this.

I found an example made in Visual Basic years ago to give you an idea of what I want to achieve:

Original VB code :)
B4X:
RichTextBox1.SelectionColor = Color.Blue

        Dim word As String = "potato"

        If RichTextBox1.Find(word) > 0 Then

            Dim position As Integer = RichTextBox1.Find(word)
            RichTextBox1.SelectionStart = position
            RichTextBox1.SelectionLength = word.Length
            RichTextBox1.SelectionColor = Color.Blue

            RichTextBox1.SelectionStart = RichTextBox1.Text.Length

        End If

Any advice is very appreciated. Thanks
 

ThRuST

Well-Known Member
Licensed User
Any clues how you solved it another way? Since this forum uses it I suspect that PHP and CSS is faster than loops in a textbox. I really want to learn how you made it work. If you at least tell what way it's done we can figure out our own way. I can even pay you for giving away the solution since your solution is brilliant :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
I had no clue about that. I guess someone came up with a more effective solution than that of a looping in a textbox. Thanks for sharing I will look that up.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Actually I am interested how to implement text color highlighting into a HTMLEditor textbox for B4J in this case and since a htm page is used I guess it could be done.
The solution provided by Daestrum might be possible to use as it supports Java, but I did not see B4J in that list, which means someone will have to create a
library for B4J to be able to use it if I'm not misstaken. Unfortunattelly I am not strong in JAVA to do it myself. Any solution that will be compatible with a HTML page is of interest. A simple example is better than nothing because right now I am lost :eek:
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
How can I do that with a Webview? I assume you mean to handle the coloring online as a backend solution, is that what you suggest?
May I know how you got that to work in the IDE, also how it works in the forum? Help me to learn this, because I will help you promote B4X.
Your products must become the new standard in the academic and corporate world. I love your products, my new project will help you be rich :)
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
How can I do that with a Webview?
The forum doesn't highlight the text while you edit it. Only when you commit the post it parses the string and converts it to a html text with colors. It shouldn't be too difficult to do the same thing inside your app.

The IDE itself uses a third party library (Actipro) to highlight the text.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Erel, how would you parse a string from HTMLEditor to achieve just that, and color only some words on the fly or in the background. It does not really matter which way the most important thing is how to make it work. Maybe it's better with a webview but then the textarea must be on a server and handled by PHP or something. My application is platform independent (B4J) and not limited to NET framework.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Thank you DarkMann, that looks very interesting I will try it. So far I only know of HTMLEditor as a competent control to display formatted HTM documents, but it's not always suitable. I found a link to a webpage about how to save generated HTML code from a HTMLEditor control. However it's in native JAVA so it would be great if someone can turn it into a library for B4J as it might come in handy. It seemed relevant to this post so I share it Here
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Just
B4X:
Dim s As String=HTMLEditor1.HtmlText
and Save to a file
B4X:
File.WriteString(File.DirApp,"htmlcode.html", s)

B4X:
Log(s)

<b>Hello</b> world!<br/>
Second line.<br/>
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Impressive inakigarm. The RichTextFX is really good. The source code example highlights colored text on the fly which means it can be used direct out of the box by using CSS in a RichTextBox which invites for using addon tools of any choice. The concept can possibly be ported to WPF for those who prefer NET framework.
I hereby thank you all. You guys are awesome. I would like to add what Steve Jobs would have said "The moment you realize that a community of people who are just as smart as yourself can cooperate to solve anything, wouldn't that be the most important thing... hmm" © 2016 Roger Lindfors ;)
 
Last edited:
Upvote 0
Top