Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Using the richTextBox control, how to change the background color of words that are separated by a comma while the user is typing?
I tried this, but all the text' backgroud of the richTextBox change, even the comma, I don't want to highlightx the comma :
C#
int lastStart = 0;
int lastEnd = 0;
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.Select(lastStart, lastEnd + 1);
            Match keyword = Regex.Match(richTextBox1.Text, @"([A-Za-z0-9\-]+)\,$",
        RegexOptions.IgnoreCase);
            if (keyword.Success)
            {
                richTextBox1.SelectionBackColor = Color.Gray;
                lastStart = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            }
            else
            {
                richTextBox1.SelectionBackColor = Color.White;
            }

            lastEnd = richTextBox1.SelectionStart + richTextBox1.SelectionLength;
            richTextBox1.Select(lastEnd, 1);
        }
Posted
Updated 2-Jul-13 12:01pm
v4
Comments
Sergey Alexandrovich Kryukov 2-Jul-13 11:39am    
Real time?! Do you know what runtime is?
—SA
Nnorss 2-Jul-13 17:57pm    
The runtime duration of this case is the period when the user is typing. If this one types 100 words (for ex), the runtime duration will be approximatly 5 minutes so I think we can use "Real time". You don't agree?
Sergey Alexandrovich Kryukov 2-Jul-13 19:11pm    
No... :-)
It's not the matter of agreement, it's just terminology. If you want to know what people normally call "real time" read about it in encyclopedia, for example...
Of course, you are free to call black white and white black, but it's hard to expect people's understanding. :-)
—SA
Nnorss 2-Jul-13 20:13pm    
Yes it's a terminology, but i think this doesn't involve that you downvote the question. By the way you downvoted all my questions, an act that will not really motivate a new programmer bearing in ming that a solution for my problem can helps another one in the future ... It seems now that it's a personal matter for you against me... exept if you're a moderator of the the website and that your task is to ensure the quality and the relevancy of the answered questions, and even if it's the case, youre reaction was very decouraging. If you were in my place, wich term will you use instead of 'real time'?
Sergey Alexandrovich Kryukov 2-Jul-13 23:45pm    
My purpose is to help, not to encourage those who feel they cannot encourage themselves. I thought you are not a child, to get any negative feedback as the opportunity to improve yourself, not as a personal attack as you probably mistakenly perceive it.

In this case, it is supposed to help you to get help, where using proper words is quite important in practice. Too many inquirers here don't get help just because many people knowledgeable in some subject don't have enough patience to read some ugly text to the end. And who do you think loose here? It's up to you to follow my advice or not — depending on what you want: to stay encouraged or really get some result.

As to the essence of your problem, I think it's the time to suggest you something useful — I was thinking at your problem while driving home from work today and remembered something relevant...

—SA

1 solution

Here is the idea: I don't think using RichTextBox, and RTF in general would be really useful here. RTF format, being the standard, is quite ugly and hard to work with; importantly, it's majorly obsolete and rarely used these days for anything more or less complex. One other problem is that, traditionally, Microsoft API for RTF was never comprehensive and allows you much less than the format can actually carry (which you can easily see if you past some complex content in it: it will be rendered, but cannot be generated via the API). It looks like Microsoft never considered the RTF APIs seriously. Maybe, RTF in principle still can be used, hard to say…

Another thing is: I saw a number of syntax highlighting components, and none of them used RTF. It also tells me something. People used very different approaches, so my point is: if you want syntax highlighting, maybe you need to try to use something which other people use?

Here is what I would advise:

First syntax highlighting component I would point to comes as a part of well-known open-source IDE written in C# called SharpDevelop:
http://en.wikipedia.org/wiki/SharpDevelop[^],
http://www.icsharpcode.net/OpenSource/SD/Default.aspx[^].

The editor component itself is called AvalonEdit: https://github.com/icsharpcode/SharpDevelop/wiki/AvalonEdit[^].

This is the WPF component, so I would recommend using WPF. It might not be the option for you. (Did you tag what are you using right now? Yes, you did. Very good, and thank you.) In this case, you can dig into earlier versions of SharpDevelop written in System.Windows.Forms, where the similar component was also available and I remember it worked quite well.

In all cases, the components can work with wide spectrum of features you can describe as custom syntax.

Another text editing component with syntax highlighting I know is Scintilla:
http://en.wikipedia.org/wiki/Scintilla_%28editing_component%29[^],
http://www.scintilla.org/[^].

As it's written in C++, you would need a .NET wrapper to use it. Here is the available one:
http://scintillanet.codeplex.com/[^].

I found syntax highlighting with Scintilla somewhat difficult, but maybe I just did not get into it well enough. :-)

Good luck,
—SA
 
Share this answer
 
v2
Comments
Nnorss 3-Jul-13 1:37am    
Big respect Sergey. I think you must start writing a book about programming and thank you.
Sergey Alexandrovich Kryukov 3-Jul-13 9:10am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900