473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2285
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

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

Similar topics

2
by: Thorsten moeller | last post by:
Hi, i have a Textbox and amethod adding lines to the Textbox. There are two kind lines, depending on a check to be valid or invalid. Is it possible to color the lines or the text in a textbox...
1
by: Moondog | last post by:
My string looks something like this: strCaption = "Item Number " & strItemNumber I would like to present the strItemNumber portion in a different color than the rest of the string so it...
1
by: Gidi | last post by:
Hi, Is it possible to Disable editing of one specific cell in one specific row in dataGrid? Thanks, Gidi.
12
by: Yi Xing | last post by:
Hi All, I want to read specific lines of a huge txt file (I know the line #). Each line might have different sizes. Is there a convenient and fast way of doing this in Python? Thanks. Yi Xing
7
by: foolproofplan | last post by:
I have a somewhat simple task I need to do, but since I am new at xml, I need help: Right now, I have xml files that are output from tests I do with an automated testing program. I want to...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
7
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to change the font color and weight at a specific position in the text. I am not sure where or what I need to do. It will be at position 155/156 from the left on each line. Here is the...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
2
by: masha2011 | last post by:
Hello all, I am very new to programming and I have recently started using python. I am interested in creating a file to essentially compare data points. I am curious if it is possible to read...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.