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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Select word in richtextbox control

Select word in richtextbox control

Scheduled Pinned Locked Moved C#
question
13 Posts 6 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.
  • S salamonty86

    Dear all, I want to select the second word only in a richtextbox control on a form. I mean if i have text like "word word word" in richtextbox i want to select the second word only. Any suggestions?? Thanks and best regards

    T Offline
    T Offline
    TheDudeJuan
    wrote on last edited by
    #4

    Split the string by " " (space) and select the second value

    Dollartegn 8D

    S 1 Reply Last reply
    0
    • realJSOPR realJSOP

      If all three words are "word", how do you know you selected the desired word?

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #5

      He's going to spell the one in the middle wrong. :laugh:

      All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • T TheDudeJuan

        Split the string by " " (space) and select the second value

        Dollartegn 8D

        S Offline
        S Offline
        salamonty86
        wrote on last edited by
        #6

        The issue is how i will select the second "word" ONLY, when i say id=2, OR select the third "word" ONLY, when i say id=3, if i have in my richtextbox string like this "word word word word word word word word word word word"

        D 1 Reply Last reply
        0
        • S salamonty86

          The issue is how i will select the second "word" ONLY, when i say id=2, OR select the third "word" ONLY, when i say id=3, if i have in my richtextbox string like this "word word word word word word word word word word word"

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #7

          DudeJuan already answered your question something like:

          string[] words = richTextBox1.Text.Split(" ");
          //get the second word
          string secondWord = words[1];

          It works for your example. But there are more separators than just space. There's also ".",";","," and many other words separators.

          S 1 Reply Last reply
          0
          • D Dan Mos

            DudeJuan already answered your question something like:

            string[] words = richTextBox1.Text.Split(" ");
            //get the second word
            string secondWord = words[1];

            It works for your example. But there are more separators than just space. There's also ".",";","," and many other words separators.

            S Offline
            S Offline
            salamonty86
            wrote on last edited by
            #8

            I understood this but what i want exactly is the selection operation in the richtextbox. How i will find and select the second "word" only without select the first occurence of it :-D

            D 1 Reply Last reply
            0
            • S salamonty86

              I understood this but what i want exactly is the selection operation in the richtextbox. How i will find and select the second "word" only without select the first occurence of it :-D

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #9

              well you need to find the location of the second " " in the text as well as the loacation of the third " " and then use the

              richTextBox1.Select(StartPosition, Endposition);

              Here are the steps: -find the start position of the nth word -find end postion of the nth word and select it. How you do that it's up to you. If it's just spaces that separates your words, then it's quite simple. id=3 => third word

              for(int i=0;i<(id-1);i++)
              {
              startPos+=words[i].Length;
              }
              //now add to the position for the spaces between the numbers
              if(id>2) startPos+=(id-2);

              for the endPosition:

              endPos = startPos+(words[id-1].Length);

              Hope it helps

              S 1 Reply Last reply
              0
              • D Dan Mos

                well you need to find the location of the second " " in the text as well as the loacation of the third " " and then use the

                richTextBox1.Select(StartPosition, Endposition);

                Here are the steps: -find the start position of the nth word -find end postion of the nth word and select it. How you do that it's up to you. If it's just spaces that separates your words, then it's quite simple. id=3 => third word

                for(int i=0;i<(id-1);i++)
                {
                startPos+=words[i].Length;
                }
                //now add to the position for the spaces between the numbers
                if(id>2) startPos+=(id-2);

                for the endPosition:

                endPos = startPos+(words[id-1].Length);

                Hope it helps

                S Offline
                S Offline
                salamonty86
                wrote on last edited by
                #10

                YES IT HELPED :rose: :rose: :rose: THANK YOUUUU VERY VERY MUCH MOS DAN

                D M 2 Replies Last reply
                0
                • S salamonty86

                  YES IT HELPED :rose: :rose: :rose: THANK YOUUUU VERY VERY MUCH MOS DAN

                  D Offline
                  D Offline
                  Dan Mos
                  wrote on last edited by
                  #11

                  you're welcome

                  1 Reply Last reply
                  0
                  • S salamonty86

                    YES IT HELPED :rose: :rose: :rose: THANK YOUUUU VERY VERY MUCH MOS DAN

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #12

                    salamonty86 wrote:

                    YES IT HELPED

                    So give a vote on the answer! Then we can all see at a glance that there is a response that was useful

                    Never underestimate the power of human stupidity RAH

                    S 1 Reply Last reply
                    0
                    • M Mycroft Holmes

                      salamonty86 wrote:

                      YES IT HELPED

                      So give a vote on the answer! Then we can all see at a glance that there is a response that was useful

                      Never underestimate the power of human stupidity RAH

                      S Offline
                      S Offline
                      salamonty86
                      wrote on last edited by
                      #13

                      Ok :) Sorry for being late :laugh:

                      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