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. Capturing the Current Line of Text from RichTextBox [modified]

Capturing the Current Line of Text from RichTextBox [modified]

Scheduled Pinned Locked Moved C#
databasequestionworkspace
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.
  • B Offline
    B Offline
    bneacetp
    wrote on last edited by
    #1

    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;

    A 1 Reply Last reply
    0
    • B bneacetp

      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;

      A Offline
      A Offline
      Abisodun
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • A Abisodun

        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.

        B Offline
        B Offline
        bneacetp
        wrote on last edited by
        #3

        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;

        A 1 Reply Last reply
        0
        • B bneacetp

          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;

          A Offline
          A Offline
          Abisodun
          wrote on last edited by
          #4

          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.

          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