469,622 Members | 1,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

How to color specific lines in richTextBox1 according to specific string word?

Expand|Select|Wrap|Line Numbers
  1. public void logger_reader(string logger_reader)
  2.         {
  3.             string  item = @"Last";
  4.             //*** Coloring all the text inside the richTextBox1 in Red ***\\
  5.             string[] ftext = File.ReadAllLines(logger_reader);
  6.             for (int i = 0; i < ftext.Length; i++)
  7.             {
  8.                 richTextBox1.SelectionColor = Color.Red;
  9.                 richTextBox1.GetFirstCharIndexFromLine(i);
  10.                 richTextBox1.SelectedText=(ftext[i] + "\r\n");
  11.             }
  12.  
So this code will color in Red all the text inside the richTextBox1. But now i want to color in Red all the lines that the word "Last" ( string item ) appears and to color all the lines from start to end that the word is appears there. Not to color in Red only the word it self "Last" but color in Red the whole line containing the word "Last" and all the other lines that wont contain this word will stay in black. And please use my code here.


Thank you very much for the help.
Jan 12 '11 #1
15 2108
Christian Binder
218 Expert 100+
I think you only missed an if-statement within the for-loop.
e.g.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < ftext.Length; i++)
  2. {
  3.   //check if line contains the word last (string item)
  4.   if(ftext[i].Contains(item)) {
  5.     //do the coloring as above
  6.   }
  7. }
  8.  
Jan 12 '11 #2
Christian Binder i did what you said but still its painting all the text inside the richTextBox1 in Red.

This is what i changed now:

Expand|Select|Wrap|Line Numbers
  1. public void logger_reader(string logger_reader)
  2.         {
  3.             string  item = @"Last";
  4.             //*** Coloring all the text inside the richTextBox1 in Red ***\\
  5.             string[] ftext = File.ReadAllLines(logger_reader);
  6.  
  7.             for (int i = 0; i < ftext.Length; i++)
  8.             {
  9.                 if (ftext[i].Contains(item))
  10.                 {
  11.                     richTextBox1.SelectionColor = Color.Red;
  12.                     richTextBox1.GetFirstCharIndexFromLine(i);
  13.                     richTextBox1.SelectedText = (ftext[i] + "\r\n");
  14.                 }
  15.             }
  16.  
Jan 12 '11 #3
Christian Binder
218 Expert 100+
I tried a few things and figured out, that first the selection has to be done and after the selection you've to set the SelectionColor to color the selected text.

Also I didn't have used SelectedText but SelectionStart and SelectionLength, maybe it works also with SelectedText.
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < ftext.Length; i++) {
  2.   int start = richTextBox1.GetFirstCharIndexFromLine(i);
  3.   richTextBox1.SelectionStart = start;
  4.   richTextBox1.SelectionLength = ftext[i].Length;
  5.  
  6.   //When keyword is found, color selection red otherwise black
  7.   if (ftext[i].Contains(item)) 
  8.     richTextBox1.SelectionColor = Color.Red;
  9.   else
  10.     richTextBox1.SelectionColor = Color.Black;
  11. }
  12.  
Jan 12 '11 #4
Christian Binder i did it as you did but im getting now error exception on the line:

Expand|Select|Wrap|Line Numbers
  1. richTextBox1.SelectionStart = start;
  2.  
The error say : InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
Parameter name: SelectionStart


And this is the code i did same as oyurs so i dont understand why im getting this exception:


The code it self :

Expand|Select|Wrap|Line Numbers
  1. public void logger_reader(string logger_reader)
  2.         {
  3.             string  item = @"Last";
  4.             //*** Coloring all the text inside the richTextBox1 in Red ***\\
  5.             string[] ftext = File.ReadAllLines(logger_reader);
  6.  
  7.             for (int i = 0; i < ftext.Length; i++)
  8.             {
  9.                   int start =  richTextBox1.GetFirstCharIndexFromLine(i);
  10.                     richTextBox1.SelectionStart = start;
  11.                     richTextBox1.SelectionLength = ftext[i].Length;
  12.  
  13.                     if (ftext[i].Contains(item))
  14.                         richTextBox1.SelectionColor = Color.Red;
  15.                     else
  16.                         richTextBox1.SelectionColor = Color.Black; 
  17.  
  18.             }
  19.  
Maybe cuz my text file wich is logger_reader is big like 127kb?

Anyway i dont know how to fix this exception.



The full exception if its important :

Expand|Select|Wrap|Line Numbers
  1. The full exception:
  2.  
  3. System.ArgumentOutOfRangeException was unhandled
  4.   Message=InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
  5. Parameter name: SelectionStart
  6.   Source=System.Windows.Forms
  7.   ParamName=SelectionStart
  8.   StackTrace:
  9.        at System.Windows.Forms.TextBoxBase.set_SelectionStart(Int32 value)
  10.        at WindowsFormsApplication1.Logger_Text_Reader.logger_reader(String logger_reader) in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Logger_Text_Reader.cs:line 36
  11.        at WindowsFormsApplication1.Form1.View_Log_File_Click(Object sender, EventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Form1.cs:line 946
  12.        at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  13.        at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  14.        at System.Windows.Forms.Control.WndProc(Message& m)
  15.        at System.Windows.Forms.ButtonBase.WndProc(Message& m)
  16.        at System.Windows.Forms.Button.WndProc(Message& m)
  17.        at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  18.        at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  19.        at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  20.        at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  21.        at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  22.        at WindowsFormsApplication1.Program.Main() in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Program.cs:line 27
  23.        at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  24.        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  25.        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  26.        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  27.        at System.Threading.ThreadHelper.ThreadStart()
  28.   InnerException: 
  29.  
Jan 12 '11 #5
Christian Binder
218 Expert 100+
Do you fill the richTextBox?
I filled it up before by using
richTextBox1.Text = File.ReadAllText(@"C:\xxx.txt");
Jan 12 '11 #6
Christian i do fill the richtextobx1 liek that:

string[] ftext = File.ReadAllLines(logger_reader);

But ill change it to how you did it.
I just wonder then in your code above where from did you take the ftext i used?
Cuz when your filling the richtextbox1 there is no ftext.

Thanks.
Jan 12 '11 #7
Christian Binder
218 Expert 100+
ftext is just a variable, which has nothing to do with my RichTextBox.
I just used it for having the lines.
So at least, I load the file twice, the first time into the RichTextBox, the second time into the ftext-array.
Jan 12 '11 #8
Christian This is the code now i change the filling of the richTextBox1 as you did.
But what do i do with the ftext im getting error on all the places since its not exist with what should i replace it now?


Expand|Select|Wrap|Line Numbers
  1. public void logger_reader(string logger_reader)
  2.         {
  3.             string  item = @"Last";
  4.             //*** Coloring all the text inside the richTextBox1 in Red ***\\
  5.           //  string[] ftext = File.ReadAllLines(logger_reader);
  6.             richTextBox1.Text = File.ReadAllText(logger_reader);
  7.             for (int i = 0; i < ftext.Length; i++)
  8.             {
  9.                   int start =  richTextBox1.GetFirstCharIndexFromLine(i);
  10.                     richTextBox1.SelectionStart = start;
  11.                     richTextBox1.SelectionLength = ftext[i].Length;
  12.  
  13.                     if (ftext[i].Contains(item))
  14.                         richTextBox1.SelectionColor = Color.Red;
  15.                     else
  16.                         richTextBox1.SelectionColor = Color.Black; 
  17.  
  18.             }
  19.  
The logger_reader getting from Form1 logger.txt wich is 127kb text file so the richTextBox1 is now filled same wya you did just what to do with the ftext rplace it with what?

Thanks.
Jan 12 '11 #9
Christian Binder
218 Expert 100+
You've to use both lines. So uncomment the commented-out line.

Expand|Select|Wrap|Line Numbers
  1. //*** Coloring all the text inside the richTextBox1 in Red ***\\
  2. string[] ftext = File.ReadAllLines(logger_reader);
  3. richTextBox1.Text = File.ReadAllText(logger_reader);
  4. for (int i = 0; i < ftext.Length; i++)
  5. ...
  6.  
Jan 12 '11 #10
Christian i used now with the two fillings lines the array ftext and the second one and what it does when i open the log file ( richTextBox1 ) i see its starting from the middle of the file and all the above part is colored with Red where there is "Last" but also many lines with "Last" are black.
And all the other text from this point the middle and bottom to the end of the text is colored with black even there are lines with "Last" in it.
Jan 12 '11 #11
Christian Binder
218 Expert 100+
That's strange, I tried it with your code above and it worked correctly.
Maybe it's because of the file-size?
Try a test-file with just a few contents (e.g. 10 lines) and look if it works?

Did you set any parameters of richTextBox1 in the Designer?
Jan 12 '11 #12
Christinan wich parameters should i set in the deisginer about the richTextBox1? And set them to what?

And i tested now the same file but with few lines the file size is 1kb and its working!
But my original file is 174kb and then its not working good.

Any ideas how to solve the size problem?

Thanks.
Jan 12 '11 #13
Christian Binder
218 Expert 100+
I don't know :-(
Can you post your file here, so I can try it?
Jan 12 '11 #14
Here is a small video i did show what happening when using the big file 127kb.

http://www.youtube.com/watch?v=ARO3mQLGZow

Short small video.

And here is a link to the logger.txt file im using the big file wich not working:

http://www.megaupload.com/?d=L8RP9X7G

And the file is 170kb not 127kb but the problem is the size.

Thanks.
Jan 12 '11 #15
Christian Binder
218 Expert 100+
I found out, that if the RichTextBox is big enough that no line is wrapped,
it works great.
Only if the richtextbox is smaller than some lines, wrong text gets colored ...
Jan 12 '11 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Thorsten moeller | last post: by
12 posts views Thread by Yi Xing | last post: by
7 posts views Thread by foolproofplan | last post: by
7 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
2 posts views Thread by Francesco Pietra | last post: by
reply views Thread by Francesco Pietra | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.