|
-
February 8th, 2005, 05:25 PM
#1
Could you make it faster?
In a very large richTextBox I want to find all letters.
Is there something faster than :
char[] letters={ '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'} ;
richTextBox1.Text.IndexOfAny(letters, i);
OR
richTextBox1.Text[i].IsLetter;
Last edited by ektoras; February 8th, 2005 at 05:55 PM.
-
February 8th, 2005, 05:30 PM
#2
Re: Could you make it faster?
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?
-
February 8th, 2005, 05:42 PM
#3
Re: Could you make it faster?
Exactly - what are you really trying to do ?
Darwen.
-
February 8th, 2005, 06:18 PM
#4
Re: Could you make it faster?
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;
}
}
Last edited by ektoras; February 8th, 2005 at 06:21 PM.
-
February 8th, 2005, 06:43 PM
#5
Re: Could you make it faster?
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 :
Code:
int nSelectionStart = richTextBox1.SelectionStart;
int nSelectionLength = richTextBox1.SelectionEnd;
richTextBox1.Select(0, richTextBox1.Text.Length);
richTextBox1.SelectionColor = Color.Blue ;
richTextBox1.Select(nSelectionStart, nSelectionLength);
Darwen.
Last edited by darwen; February 8th, 2005 at 06:46 PM.
-
February 8th, 2005, 07:05 PM
#6
Re: Could you make it faster?
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?)
-
February 8th, 2005, 07:59 PM
#7
fast way to find the indexof the first letter in a richTextBox?
fast way to find the index of the first letter in a richTextBox?
Last edited by ektoras; February 9th, 2005 at 05:28 AM.
-
February 8th, 2005, 09:37 PM
#8
Re: Could you make it faster?
Are you suppressing updating while you are making the changes????
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 9th, 2005, 03:17 AM
#9
Re: fast way to find the first letter in a richTextBox?
If you think you CAN, you can, If you think you CAN'T, you are probably right.
Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.
-
February 9th, 2005, 04:25 AM
#10
Re: Could you make it faster?
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
http://www.codeguru.com/Cpp/controls...cle.php/c2387/
Source is c++ though. Algorithms could be the same however.
Another hit on google is
http://www.codeproject.com/vb/net/re...axcoloring.asp
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
Last edited by Anders; February 9th, 2005 at 04:28 AM.
-
February 9th, 2005, 05:37 AM
#11
Re: fast way to find the first letter in a richTextBox?
Thank You Very-very Much Anders!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|