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. How do I Find characters in a single line of a richtextbox

How do I Find characters in a single line of a richtextbox

Scheduled Pinned Locked Moved C#
regexhelpquestion
5 Posts 3 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.
  • I Offline
    I Offline
    I_Need_Help
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • I I_Need_Help

      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

      D Offline
      D Offline
      duncanmhor
      wrote on last edited by
      #2

      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!"

      I 1 Reply Last reply
      0
      • D duncanmhor

        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!"

        I Offline
        I Offline
        I_Need_Help
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • I I_Need_Help

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          I 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            I Offline
            I Offline
            I_Need_Help
            wrote on last edited by
            #5

            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

            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