Capturing the Current Line of Text from RichTextBox [modified]
-
I am working on a program which loads rich text documents into a RichTextBox control. What I need is to grab the text of the current "line" (including any text that may have wrapped around to the following line) that I have my insertion point at in the text. I had thought that I might be able search backward as well as forward for the closest System.Environment.NewLine (capturing the index of each) and then do a Sting.Substring call using the indexes of each NewLine find, but for some reason, that is not working. Are there any suggestions for how I could do this with greater success? :confused: Thanks in advance. -- modified at 23:38 Thursday 26th July, 2007
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life." using Earth.Internet.WWW.CodeProject;
-
I am working on a program which loads rich text documents into a RichTextBox control. What I need is to grab the text of the current "line" (including any text that may have wrapped around to the following line) that I have my insertion point at in the text. I had thought that I might be able search backward as well as forward for the closest System.Environment.NewLine (capturing the index of each) and then do a Sting.Substring call using the indexes of each NewLine find, but for some reason, that is not working. Are there any suggestions for how I could do this with greater success? :confused: Thanks in advance. -- modified at 23:38 Thursday 26th July, 2007
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life." using Earth.Internet.WWW.CodeProject;
Two kinds of lines in RichTextBox: 1) The lines separated by newline characters i.e. richTextBox.Lines[] 2) Lines that wrap in the control Get 2) using GetFirstCharIndexOfCurrentLine, GetLineFromCharIndex(richTextBox.SelectionStart) etc A combination of these functions and properties should be what you need. Good Luck.
-
Two kinds of lines in RichTextBox: 1) The lines separated by newline characters i.e. richTextBox.Lines[] 2) Lines that wrap in the control Get 2) using GetFirstCharIndexOfCurrentLine, GetLineFromCharIndex(richTextBox.SelectionStart) etc A combination of these functions and properties should be what you need. Good Luck.
Thanks for your suggestions. It looks like they have gotten me closer to what I am trying to achieve. Take the below example:
Title
This is the third line of text.
This is the fourth line of text that
by example would wrap to the next line.Currently, when having my insertion point in any of the first four lines, the result is as expected: I only get the line that I have my insertion point at. When I venture to the fifth line which is really for the purposes of this example a wrapped continuation of the fourth, it counts as the fifth line. How do I tell the difference between a line that is the result from wrapping text and one that is divided by a new line/return carriage? Thanks in advance.
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life." using Earth.Internet.WWW.CodeProject;
-
Thanks for your suggestions. It looks like they have gotten me closer to what I am trying to achieve. Take the below example:
Title
This is the third line of text.
This is the fourth line of text that
by example would wrap to the next line.Currently, when having my insertion point in any of the first four lines, the result is as expected: I only get the line that I have my insertion point at. When I venture to the fifth line which is really for the purposes of this example a wrapped continuation of the fourth, it counts as the fifth line. How do I tell the difference between a line that is the result from wrapping text and one that is divided by a new line/return carriage? Thanks in advance.
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life." using Earth.Internet.WWW.CodeProject;
I'm sure there are several solutions but my first thought is as follows: e.g. first second third fourth that wraps fourth that wraps fourth that wraps fourth that wraps string[] lines = richTextBox.Lines; i.e. lines[0] returns "first", lines[3] returns "fourth that wraps fourth that wraps fourth that wraps fourth that wraps". 1) Create a class LineClass with properties: int FirstCharacter, int LastCharacter, string Line. 2) Add an entry for each line to a List. Remember there is a return character between lines so that in my example: first instance of LineClass FirstCharacter=0, LastCharacter=4, Line=first second instance of LineClass FirstCharacter=6, LastCharacter=11, Line=second 3) Use GetFirstCharIndexOfCurrentLine to determine the cursor location. 4) Loop through List to get the line you need. If the line is a result of wrapping, your answer in 3) will fall between FirstCharacter and LastCharacter for that entry. Otherwise it will equal FirstCharacter. Good Luck.