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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    #1

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

    Code:
    public void logger_reader(string logger_reader)
            {
                string  item = @"Last";
                //*** Coloring all the text inside the richTextBox1 in Red ***\\
                string[] ftext = File.ReadAllLines(logger_reader);
                for (int i = 0; i < ftext.Length; i++)
                {
                    richTextBox1.SelectionColor = Color.Red;
                    richTextBox1.GetFirstCharIndexFromLine(i);
                    richTextBox1.SelectedText=(ftext[i] + "\r\n");
                }
    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.
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    I think you only missed an if-statement within the for-loop.
    e.g.

    Code:
    for (int i = 0; i < ftext.Length; i++)
    {
      //check if line contains the word last (string item)
      if(ftext[i].Contains(item)) {
        //do the coloring as above
      }
    }

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      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:

      Code:
      public void logger_reader(string logger_reader)
              {
                  string  item = @"Last";
                  //*** Coloring all the text inside the richTextBox1 in Red ***\\
                  string[] ftext = File.ReadAllLines(logger_reader);
                   
                  for (int i = 0; i < ftext.Length; i++)
                  {
                      if (ftext[i].Contains(item))
                      {
                          richTextBox1.SelectionColor = Color.Red;
                          richTextBox1.GetFirstCharIndexFromLine(i);
                          richTextBox1.SelectedText = (ftext[i] + "\r\n");
                      }
                  }

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        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.
        Code:
        for (int i = 0; i < ftext.Length; i++) {
          int start = richTextBox1.GetFirstCharIndexFromLine(i);
          richTextBox1.SelectionStart = start;
          richTextBox1.SelectionLength = ftext[i].Length;
        
          //When keyword is found, color selection red otherwise black
          if (ftext[i].Contains(item)) 
            richTextBox1.SelectionColor = Color.Red;
          else
            richTextBox1.SelectionColor = Color.Black;
        }

        Comment

        • Chocolade
          New Member
          • Aug 2010
          • 69

          #5
          Christian Binder i did it as you did but im getting now error exception on the line:

          Code:
          richTextBox1.SelectionStart = start;
          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 :

          Code:
          public void logger_reader(string logger_reader)
                  {
                      string  item = @"Last";
                      //*** Coloring all the text inside the richTextBox1 in Red ***\\
                      string[] ftext = File.ReadAllLines(logger_reader);
                       
                      for (int i = 0; i < ftext.Length; i++)
                      {
                            int start =  richTextBox1.GetFirstCharIndexFromLine(i);
                              richTextBox1.SelectionStart = start;
                              richTextBox1.SelectionLength = ftext[i].Length;
          
                              if (ftext[i].Contains(item))
                                  richTextBox1.SelectionColor = Color.Red;
                              else
                                  richTextBox1.SelectionColor = Color.Black; 
          
                      }
          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 :

          Code:
          The full exception:
          
          System.ArgumentOutOfRangeException was unhandled
            Message=InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
          Parameter name: SelectionStart
            Source=System.Windows.Forms
            ParamName=SelectionStart
            StackTrace:
                 at System.Windows.Forms.TextBoxBase.set_SelectionStart(Int32 value)
                 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
                 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
                 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
                 at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
                 at System.Windows.Forms.Control.WndProc(Message& m)
                 at System.Windows.Forms.ButtonBase.WndProc(Message& m)
                 at System.Windows.Forms.Button.WndProc(Message& m)
                 at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
                 at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
                 at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
                 at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
                 at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
                 at WindowsFormsApplication1.Program.Main() in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Program.cs:line 27
                 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
                 at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
                 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
                 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
                 at System.Threading.ThreadHelper.ThreadStart()
            InnerException:

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            Do you fill the richTextBox?
            I filled it up before by using
            richTextBox1.Te xt = File.ReadAllTex t(@"C:\xxx.txt" );

            Comment

            • Chocolade
              New Member
              • Aug 2010
              • 69

              #7
              Christian i do fill the richtextobx1 liek that:

              string[] ftext = File.ReadAllLin es(logger_reade r);

              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.

              Comment

              • Christian Binder
                Recognized Expert New Member
                • Jan 2008
                • 218

                #8
                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.

                Comment

                • Chocolade
                  New Member
                  • Aug 2010
                  • 69

                  #9
                  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?


                  Code:
                  public void logger_reader(string logger_reader)
                          {
                              string  item = @"Last";
                              //*** Coloring all the text inside the richTextBox1 in Red ***\\
                            //  string[] ftext = File.ReadAllLines(logger_reader);
                              richTextBox1.Text = File.ReadAllText(logger_reader);
                              for (int i = 0; i < ftext.Length; i++)
                              {
                                    int start =  richTextBox1.GetFirstCharIndexFromLine(i);
                                      richTextBox1.SelectionStart = start;
                                      richTextBox1.SelectionLength = ftext[i].Length;
                  
                                      if (ftext[i].Contains(item))
                                          richTextBox1.SelectionColor = Color.Red;
                                      else
                                          richTextBox1.SelectionColor = Color.Black; 
                  
                              }
                  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.

                  Comment

                  • Christian Binder
                    Recognized Expert New Member
                    • Jan 2008
                    • 218

                    #10
                    You've to use both lines. So uncomment the commented-out line.

                    Code:
                    //*** Coloring all the text inside the richTextBox1 in Red ***\\
                    string[] ftext = File.ReadAllLines(logger_reader);
                    richTextBox1.Text = File.ReadAllText(logger_reader);
                    for (int i = 0; i < ftext.Length; i++)
                    ...

                    Comment

                    • Chocolade
                      New Member
                      • Aug 2010
                      • 69

                      #11
                      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.

                      Comment

                      • Christian Binder
                        Recognized Expert New Member
                        • Jan 2008
                        • 218

                        #12
                        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?

                        Comment

                        • Chocolade
                          New Member
                          • Aug 2010
                          • 69

                          #13
                          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.

                          Comment

                          • Christian Binder
                            Recognized Expert New Member
                            • Jan 2008
                            • 218

                            #14
                            I don't know :-(
                            Can you post your file here, so I can try it?

                            Comment

                            • Chocolade
                              New Member
                              • Aug 2010
                              • 69

                              #15
                              Here is a small video i did show what happening when using the big file 127kb.



                              Short small video.

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



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

                              Thanks.

                              Comment

                              Working...