How do I Find characters in a single line of a richtextbox
-
I have a richtextbox, each line has in it a unique 6 digit number on the line so I can find the line number but then I want to search just the given line from character position x to end of that line for a different character pattern and select them so as to allow me to change their fore and background colours. Currently I select the unique text on a line and change it's color thus rtxtResults.Select( rtxtResults.Find(texttofind),(texttofind.Length)); rtxtResults.SelectionBackColor = Color.Red; rtxtResults.SelectionColor = Color.Gold; I then want to be able to search all charicters that follow the text found above to the end of that line only. I asume I would somehow need to use rtxtResults.GetLineFromCharIndex but can’t work out a way of restricting the find to a single line in the richtextbox. Any help/sample would be apprichated. thanks
-
I have a richtextbox, each line has in it a unique 6 digit number on the line so I can find the line number but then I want to search just the given line from character position x to end of that line for a different character pattern and select them so as to allow me to change their fore and background colours. Currently I select the unique text on a line and change it's color thus rtxtResults.Select( rtxtResults.Find(texttofind),(texttofind.Length)); rtxtResults.SelectionBackColor = Color.Red; rtxtResults.SelectionColor = Color.Gold; I then want to be able to search all charicters that follow the text found above to the end of that line only. I asume I would somehow need to use rtxtResults.GetLineFromCharIndex but can’t work out a way of restricting the find to a single line in the richtextbox. Any help/sample would be apprichated. thanks
You could find the next the carriage return character, the position of that character would give you the length to search. "You're very clever, young man, very clever," said the old lady. "But it's turtles all the way down!"
-
You could find the next the carriage return character, the position of that character would give you the length to search. "You're very clever, young man, very clever," said the old lady. "But it's turtles all the way down!"
Thank you that seems such an obvious think to do, I should have thought of it too!!! however I'm haveing an issue with it in that it alway returns -1 (not found!). int endline = rtxtResults.Find(System.Environment.NewLine, 1, RichTextBoxFinds.None); or int endline = rtxtResults.Find("\r", 1, RichTextBoxFinds.None); As can be seen above even manualy setting the start location to 1 it never finds the end of line!:-O the watch show each line ending in \n ! any idea why? thanks
-
Thank you that seems such an obvious think to do, I should have thought of it too!!! however I'm haveing an issue with it in that it alway returns -1 (not found!). int endline = rtxtResults.Find(System.Environment.NewLine, 1, RichTextBoxFinds.None); or int endline = rtxtResults.Find("\r", 1, RichTextBoxFinds.None); As can be seen above even manualy setting the start location to 1 it never finds the end of line!:-O the watch show each line ending in \n ! any idea why? thanks
An RTB knows about text lines, there are a couple of methods that support that concept, such as GetFirstCharIndexFromLine() and GetLineFromCharIndex(). :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
An RTB knows about text lines, there are a couple of methods that support that concept, such as GetFirstCharIndexFromLine() and GetLineFromCharIndex(). :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
thanks for your pointer I now have it working thus: int currentline = rtxtResults.GetLineFromCharIndex(rtxtResults.Find(texttofind)); int firstcharinline = rtxtResults.GetFirstCharIndexFromLine(currentline); int lastcharonline = firstcharinline + rtxtResults.Lines[currentline].Length; rtxtResults.Select(rtxtResults.Find(nexttextofind, firstcharinline, lastcharonline, RichTextBoxFinds.None), nexttextofind.lenght); Thanks