Highlight a line that contains a string
-
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?
-
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?
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.
-
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.
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); }
-
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); }
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); }