• Advertisement
Sign in to follow this  

Highlighting Certain Areas of a Text Area

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I go about highlighting certain areas of a text area for doing something like syntax highlighting in a Java text area? The procedure may be the same in Visual C++, but I'm not sure how to accomplish something like this. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by BlueShizz
How would I go about highlighting certain areas of a text area for doing something like syntax highlighting in a Java text area?

The procedure may be the same in Visual C++, but I'm not sure how to accomplish something like this.

Thanks


It depends on how you are drawing the text.

If you are displaying the text with a control given by the OS, you would use its formatting. For an HTML control that would mean that the back end would need to parse the content and add appropriate HTML markup in the control. If it supports RTF, you'd need to similarly adjust it. That probably means having your app respond to any 'text changing' events to re-parse and re-format as needed.

If you are drawing the text yourself, you would set the text color when drawing the text.

If you provide a bigger description (what specific controls you are using, excactly what your end goals are, and your current competency) there are many of us who could offer more help on that.

Share this post


Link to post
Share on other sites
Not to dissuade you, but you might try a pre-built solution. I haven't tried their syntax editing box, but I have actually bought controls from this company before, and they worked far better than most third-party controls I've tried--hence I actually spent money on this company.

I've built a few IDE's (for Java & PHP), and I've had varying degrees of success getting syntax highlighting to work well. In my experience, syntax highlighting is a Hard Problem. You have a couple of options.

The first, and probably most common for quick projects, is to use the built-in Windows RichTextBox control. You take your original java code and convert it into RTF format, then set the RTF property for the control to this RTF code. This can work well, but generally causes a lot of quirky behavior. I quickly abandoned this option.

Another option is to build your own textbox from scratch. That way, you have total control over rendering and can thus do whatever highlighting/underlining/bolding/whatevering you want. This is extremely difficult to do, even if you only allow for fixed-width fonts, as is typical in coding environments. I've done this using both C# and C++ (MFC), and in this case neither language had a significant advantage in creation speed, since you're working at such a low level. If you're interested in some details of the algorithm I use, you can see my post in this thread. Since I posted there, I've made tons of incremental improvements to the algorithm, and now most of the kinks are worked out.

The fundamental problem with context highlighting is that you're trying to approximate the work of a compiler or interpreter in real time. A C# project may take ten seconds to compile, but if the user has to wait ten seconds between typing a letter and seeing the color of the text change, they'll quickly move to a new product. So you do a rough hack, but it works incorrectly. The user uses the characters /* inside of a string, and your context highlighter turns everything after that into a comment. Repeated struggles with this type of issue led me to the algorithm I described in my above-mentioned post.

Anyway, feel free to ask any more specific questions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement