Regular Expressions are probably what you're after, but what exactly are you trying to do? Find the letters that a word starts with, etc?
darwen
February 8th, 2005, 04:42 PM
Exactly - what are you really trying to do ?
Darwen.
ektoras
February 8th, 2005, 05:18 PM
I Want to make blue color all words. But it is not fast enough.
int position=0;
char[] KeywordsCanStartWith = {'_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'} ;
char[] KeywordsEndBeforeApears = {' ','\t','(',')',',','.',';','\n','[',']','{','}','+','-','*','/',':',
'=','<','>','|','\\','&','%','?','$','!','"','#','\'','@','^'} ;
bool LogVar;
//give blue color to all keywords(=words)
void giveColorToKeywordsinRTB()
{
position=0;
KeywordStartsHere=-1; caretPos=richTextBox1.SelectionStart;
LogVar=true;
LockWindowUpdate(richTextBox1.Handle);
richTextBox1.Select(0, richTextBox1.Text.Length);
richTextBox1.SelectionColor = Color.Black ;
while(LogVar)
{
KeywordStartsHere = richTextBox1.Text.IndexOfAny(KeywordsCanStartWith , position);
if(position<0)
break ;
if(position==KeywordStartsHere)
{
position = richTextBox1.Text.IndexOfAny(KeywordsEndBeforeApears, position) ;
if(position==-1)
position=richTextBox1.Text.Length-1 ;
else
position--;
richTextBox1.Select(KeywordStartsHere, position-KeywordStartsHere+1);
richTextBox1.SelectionColor = Color.Blue ;
}
}
if(++position > richTextBox1.Text.Length-1)
LogVar=false;
}
}
darwen
February 8th, 2005, 05:43 PM
Please, put your code in code blocks - look at the 'code' button at the top of every post put in "hello" or something and it'll tell you how to put your code in blocks.
Why can't you just make the whole string blue ? This will have the same effect - as spaces are blanks.
You might want to restore the selection afterwards of course :
int nSelectionStart = richTextBox1.SelectionStart;
int nSelectionLength = richTextBox1.SelectionEnd;
richTextBox1.Select(0, richTextBox1.Text.Length);
richTextBox1.SelectionColor = Color.Blue ;
richTextBox1.Select(nSelectionStart, nSelectionLength);
Darwen.
ektoras
February 8th, 2005, 06:05 PM
Darwen and Mmetzger It is a source code editor that makes use of
/*, */, //, "" to make some pieces green. So I must jumb from point to point.
(That's why I prefer to use .IndexOf instead of .IsLetter)
Could you give me an idea of how to use the solution of Regular Expressions (with a litle piece of code about syntax if it is possible?)
ektoras
February 8th, 2005, 06:59 PM
fast way to find the index of the first letter in a richTextBox?
TheCPUWizard
February 8th, 2005, 08:37 PM
Are you suppressing updating while you are making the changes????
Andy Tacker
February 9th, 2005, 02:17 AM
Merged two thread...
Anders
February 9th, 2005, 03:25 AM
You are doing something called "syntax coloring" or "syntax highlighting". Have you even tried to search for this topic somewhere? (which is not easy if you don't know what it is called ofcourse :))
I just did a search on syntax coloring on Google, and guess what? 4th hit points right back at our favorite site: www.codeguru.com. Complete link is
In the discussion there, you have a VERY brief description of a commercial syntax color editing control.
Testing your code, by the way, reveals an endless loop plus that you dont unlock the window update. Also, the way you do it now, you compare each character (or .NET does in IndexOfAny) to 30 or 50 other characters. This is bound to take a while. My tests shows that RegEx is faster, but you have to learn how to compose the expressions. This is documented in MSDN, and surely there are tutorials all around the net. Here's one: http://sitescooper.org/tao_regexps.html
Just trying to help :)
ektoras
February 9th, 2005, 04:37 AM
Thank You Very-very Much Anders!!!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.