Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Highlight a line that contains a string

Highlight a line that contains a string

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    clatten
    wrote on last edited by
    #1

    Since I received good answers on my previous question I'll go for a new. I have a richtextbox and like to highlight lines that contain a specific string. I'v found the .Find method which lets me search for a string and then with the selection methods I can highlight by changing font and foreground colour etc. But the problem is that only the search string becomes selected and not the whole line. Also only the first occurrence is selected but I guess that's solvable. Is there some way to achieve this with .Find or is there any other way I can/should go?

    L 1 Reply Last reply
    0
    • C clatten

      Since I received good answers on my previous question I'll go for a new. I have a richtextbox and like to highlight lines that contain a specific string. I'v found the .Find method which lets me search for a string and then with the selection methods I can highlight by changing font and foreground colour etc. But the problem is that only the search string becomes selected and not the whole line. Also only the first occurrence is selected but I guess that's solvable. Is there some way to achieve this with .Find or is there any other way I can/should go?

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      Looking at the richtext control in a prototype project I built, it indicates that display.Find() returns an index of the found line of text. So it may be possible to make use of the display.GetLineFromCharIndex() using the result form Find(); So your final code would be this:

       RichTextBox display = new RichTextBox();
        ...
       public void SetLine(string textToSelect)
       {
            int locate = display.Find(textToSelect);
            int linePos= display.GetLineFromCharIndex(locate);
            this.SetSelectedStyle();
            display.SelectedText = display.Lines\[linePos\];
       }
      

      The approach I took to determine this was to create a blank project. Add a richtext control. Create a dummy method in the project so that I can see exposed properties and methods. Start looking at 'WHAT' for the return type and description of Find(); See what is exposed that deals with lines based on character position. See what collection might be used for the output of that. ______________________________ The Tao gave birth to machine language. Machine language gave birth to the assembler. The assembler gave birth to ten thousand languages. Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao. Beauty exists because we give a name to C#. Bad exists because we give a name to COBOL.

      C 1 Reply Last reply
      0
      • L LongRange Shooter

        Looking at the richtext control in a prototype project I built, it indicates that display.Find() returns an index of the found line of text. So it may be possible to make use of the display.GetLineFromCharIndex() using the result form Find(); So your final code would be this:

         RichTextBox display = new RichTextBox();
          ...
         public void SetLine(string textToSelect)
         {
              int locate = display.Find(textToSelect);
              int linePos= display.GetLineFromCharIndex(locate);
              this.SetSelectedStyle();
              display.SelectedText = display.Lines\[linePos\];
         }
        

        The approach I took to determine this was to create a blank project. Add a richtext control. Create a dummy method in the project so that I can see exposed properties and methods. Start looking at 'WHAT' for the return type and description of Find(); See what is exposed that deals with lines based on character position. See what collection might be used for the output of that. ______________________________ The Tao gave birth to machine language. Machine language gave birth to the assembler. The assembler gave birth to ten thousand languages. Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao. Beauty exists because we give a name to C#. Bad exists because we give a name to COBOL.

        C Offline
        C Offline
        clatten
        wrote on last edited by
        #3

        OK, I tried that and it worked somehow but there's still a problem. Given the code below I search for the string "string" and then select the line that contains the string "This is the string I search for". The result will be that string is replaced and highlighted and thus giving: "This is the This is the string I search for I search for". I have tried several solutions avoiding this, but I assume I need to use SelectedText.Replace or set Select to start from the beginning of the line But I can't find out a way to calculate where the line starts and ends. Anyone that can hint how to proceed? private void WinForm_Load(object sender, System.EventArgs e) { richTextBox1.Text = "Hello world\n"; richTextBox1.AppendText("This is the string I search for\n"); richTextBox1.AppendText("The last row\n"); } } private void button1_Click(object sender, System.EventArgs e) { int locate = richTextBox1.Find("string"); int line = richTextBox1.GetLineFromCharIndex(locate); richTextBox1.SelectionColor = Color.Red; richTextBox1.SelectedText = richTextBox1.Lines[line]; string q = richTextBox1.Lines[line];; richTextBox1.SelectionProtected = true; int len = richTextBox1.Lines[line].Length; string txt = richTextBox1.Lines[line]; richTextBox1.SelectionCharOffset = 5; richTextBox1.AppendText("Line no: " + line + ", Locate: " + locate + ", Len: " + len + "\n"); richTextBox1.AppendText(txt + "\n"); richTextBox1.AppendText("q: " + q); }

        C 1 Reply Last reply
        0
        • C clatten

          OK, I tried that and it worked somehow but there's still a problem. Given the code below I search for the string "string" and then select the line that contains the string "This is the string I search for". The result will be that string is replaced and highlighted and thus giving: "This is the This is the string I search for I search for". I have tried several solutions avoiding this, but I assume I need to use SelectedText.Replace or set Select to start from the beginning of the line But I can't find out a way to calculate where the line starts and ends. Anyone that can hint how to proceed? private void WinForm_Load(object sender, System.EventArgs e) { richTextBox1.Text = "Hello world\n"; richTextBox1.AppendText("This is the string I search for\n"); richTextBox1.AppendText("The last row\n"); } } private void button1_Click(object sender, System.EventArgs e) { int locate = richTextBox1.Find("string"); int line = richTextBox1.GetLineFromCharIndex(locate); richTextBox1.SelectionColor = Color.Red; richTextBox1.SelectedText = richTextBox1.Lines[line]; string q = richTextBox1.Lines[line];; richTextBox1.SelectionProtected = true; int len = richTextBox1.Lines[line].Length; string txt = richTextBox1.Lines[line]; richTextBox1.SelectionCharOffset = 5; richTextBox1.AppendText("Line no: " + line + ", Locate: " + locate + ", Len: " + len + "\n"); richTextBox1.AppendText(txt + "\n"); richTextBox1.AppendText("q: " + q); }

          C Offline
          C Offline
          clatten
          wrote on last edited by
          #4

          Dunno what I was thinking about yesterday since it was pretty obvious. private void onLoad(object sender, System.EventArgs e) { richTextBox1.Text = "Hello World\n"; richTextBox1.AppendText("This is the string I search for\n"); richTextBox1.AppendText("Another Hello World Again\n"); } private void button1_Click(object sender, System.EventArgs e) { int loc = richTextBox1.Find("string"); int linePos= richTextBox1.GetLineFromCharIndex(loc); int lineLen = richTextBox1.Lines[linePos].Length; int lineStart = richTextBox1.Lines[linePos].IndexOf("string"); richTextBox1.Select(lineStart,lineLen); richTextBox1.SelectionColor = Color.Blue; richTextBox1.SelectedText = richTextBox1.Lines[linePos]; richTextBox1.AppendText("strPos: " + loc + ", Len: " + lineLen + ", linestart: " + lineStart); }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups